0

I was researching on ways to tackle NullPointerExceptions and the three things I found to be most useful were :

  1. Assert statement
  2. Annotations (@Nonnull, @Nullable)
  3. Using "?." as null check Example : if(MyRef != null){ MyRef.doSomething(); }

         The above code can be changed to : MyRef?.doSomething();
    

Please comment on how helpful these three above mentioned strategies are and if there are any more ways to tackle NullPointerExceptions.

Arjun Issar
  • 672
  • 4
  • 13
  • 32
  • possible duplicate of http://stackoverflow.com/questions/271526/avoiding-null-statements-in-java – JBA Mar 17 '15 at 09:24
  • 1
    As a comment to the question i just want to note that the annotation based solution is enforced on compile time to check for null and wont lead to any checks or similar code regarding null once compilation was successfull (and the null check performed) while the other two will under any circumstances be in your compiled classes and working in the runtime scope (Note this is a very general quick answer as a comment ;)) – JBA Mar 17 '15 at 09:26
  • 1. Instead of tacking with null pointer, try to find source of the same. 2. @NotNull checks are compile time, what if you start to generate null values at runtime? – SMA Mar 17 '15 at 09:27
  • Exactly, Assert statement and Annotations are compile time checks. What about null values at runtime. Please give information about "?." and what IDEs use this? – Arjun Issar Mar 17 '15 at 11:06

0 Answers0