Null reference may not be a mistakeThe null pointer is considered to be a "billion-dollar mistake". I have been wondering why there is such a notion until I saw the video where Tony Hoare claims it to be his mistake. In fact, he didn't really say that null pointer should not be used. From this video, you can see that introducing null reference is not really a mistake. On the contrary, null references are helpful and sometimes indispensable. The mistake is not in the existence of the null pointers, but in how the type system treats them. Unfortunately, most languages (C++, Java, C#, ...) don't treat them correctly. Every class type Unfortunately most languages don't provide a convenient union type that you can put {String, null} find1() { if (...) { return "okay"; } else { return null; } } This is saying: String s = find(); if (s != null) { x = s.length(); } In comparison, if we define a slightly different function find2, with a different return type: String find2() { ... return "okay"; } From the return type we know that find2 will never return null, so the type checker can let you you use the String without checking: String s = find(); x = s.length(); |