r/java • u/battlmonstr • Jun 27 '16
Null pointer exceptions hell
http://dobegin.com/npe-hell/3
Jun 29 '16 edited Jun 29 '16
Option types are known as Nullable<T>, Optional<T>, Option<T> or Maybe T in different programming languages (where “T” denotes a data type). Such type lets you express an explicit intent that a value can potentially be null and requires an explicit cast if you want to get to a contained non-null value.
Saying that Optional is something that may contain null is like saying that ArrayList of size 0 may contain null which is not true. Optional is more like collection of max 1 element. Some collections may contain null but their size must be bigger than zero first (because null is a valid element of this collection). Try this code:
ArrayList<Object> arrayList = new ArrayList<>();
arrayList.add(null);
System.out.println(arrayList.size()); // prints 1, not 0Also languages like Haskell or Rust doesn't hide null in Options. They just doesn't provide null ref/pointer. If you try to get value when it's absent you may even get problem with compiling your program. And that's nothing to do with null. Optional in Java may be used to solve similar problems but that doesn't mean they are similar concepts.
5
u/Luolong Jun 27 '16
Also Ceylon language has a fairly unique approach to null safety: http://ceylon-lang.org/documentation/1.2/introduction/#typesafe_null_and_flow_sensitive_typing
2
u/QshelTier Jun 28 '16
It’s not unique, Kotlin does that, too. See link from /u/thatsIch below.
1
u/Luolong Jun 29 '16
Well. Syntax wise the Kotlin approach does seem to have a similar flavor, but the internals of the null safety in Ceylon seem to be completely different.
From what I understand (and feel free to fix my misunderstanding if I am wrong), Kotlin achieves null safety by special casing nullable types in the type system and performing special static analyses for nullability.
Ceylon on the other hand uses same type algebra it uses for the rest of the type system to determine type of a value. Nullable references are nothing special to type checker - they are simply union types of Null|T and all the same rules apply as to any other union types.
1
u/QshelTier Jun 30 '16
Yes, the implementation is different but on the surface it looks the same, what with smart casts after null checks and what not…
0
Jun 29 '16
[deleted]
0
0
u/QshelTier Jun 30 '16
That’s quite unfair. Ceylon is pretty neat; it’s a shame it never really caught on…
1
u/QshelTier Jun 28 '16
The billion dollar mistake was to terminate C-strings with NUL. That, however, has nothing to do with a null pointer exception.
0
u/lukaseder Jun 29 '16
Conclusion: Use SQL to avoid NullPointerExceptions
1
-4
u/yogazook Jun 28 '16
And yet they're the easiest exceptions to avoid. It's actually pretty much programming 101: 1) don't return null, and/or 2) check to make sure it's not null before you do something that can throw an NPE.
var element = null;
element.getAttribute("id");
// crashYeah, no shit.
1
u/battlmonstr Jun 28 '16
Sure. My point is that if you do something really dumb, then the compiler/parser should be able to stop you and warn you until it's too late. When you fix this bug for 1000th time it's not funny anymore. It's often not your own code.
1
u/yogazook Jun 29 '16
Better than the compiler is a good IDE, such as IntelliJ. Better than that is a decent test suite, with test methods like
public void should_not_blow_up_if_x_is_null(){ }1
u/battlmonstr Jun 29 '16
Unit testing is a possible approach, but I don't quite see how it's better than a simple "if (x != null)" check (you need it anyway probably). When adding such a test you need to think about it yourself upfront. This test certainly reassures that it's not null, and helps with regressions. You could also add assertions/preconditions for that matter.
IntelliJ is great, I use it all the time, but in its core it is only as good as the language itself (maybe a little bit better).
1
u/QshelTier Jun 30 '16
Checking for
nullis okay (well, not really, it sucks) but having a compiler that refuses to create a binary if you forget it is better.
7
u/thatsIch Jun 27 '16
There is also Kotlin as a good example how to handle NPEs
https://kotlinlang.org/docs/reference/null-safety.html