Linked Questions
167 questions linked to/from How do I avoid checking for nulls in Java?
17 votes
2 answers
22k views
null-safe Collection contains method
What's the best way to do null-safe contains on a Java collection? in other words - if (collection != null && collection.contains(x)) ? I was hoping Apache commons-collections had ...
1 vote
6 answers
5k views
Null check coding standard [duplicate]
I have a doubt regarding coding standard of a null check. I want to know the difference between if(a!=null) and if(null!=a) which one is better,which one to use and why?
7 votes
4 answers
11k views
How to avoid returning NULL value in a function (JAVA)?
Supposed I have a List<SomeObject> and have a function that return the reference of that object if available. SomeObject GetSomeObject(List<SomeObject>, int x){ /* Search for object ...
9 votes
3 answers
24k views
Assert for null check
It seems widely accepted that assert statements should be reserved for testing and disabled in production because errors should have been resolved by then, and enabling assertions affects performance. ...
8 votes
4 answers
14k views
Is it a good or bad practice to check for NULL? [closed]
I have seen code where almost every variable in all application layers is checked for not being null. I have also seen code almost without this. if(object != null){} What are the best practices for ...
8 votes
2 answers
17k views
where is NotNull in java? [duplicate]
I want to compile simple code with oracle jdk 1.8: class Foo { public static void test(@NotNull String a) {} } But idea not understand such code, and suggest to add: import com.sun.istack....
2 votes
7 answers
2k views
How to check any two parameters of a function are null
Suppose I have a function that takes three parameters. Of course I could check that any two parameters of this function is null in this way. returnType function(p1, p2, p3){ if((p1 == null &&...
8 votes
3 answers
3k views
What is the best alternative to null in object oriented programming?
I don't feel satisfied by avoiding null in OOP. Is there any alternative solution? I don't like to avoid it in this way either. What is the best possible way to handle it?
5 votes
4 answers
5k views
Is it OK to use Assert as preconditions?
I was going through this post related to handling of nulls. One of the recommendation (as per the SO post) is to use Assert when nulls are not valid. I have (so far) used nulls extensively in Test ...
0 votes
6 answers
7k views
is it bad practice to check !(x == null)
Is this bad practice or any performance hit, this is to check x is not equal to null if( !(x == null) ){ System.out.println("x is not a null value"); }
1 vote
5 answers
1k views
Is there any way to auto catch exception?
Suppose I have 10 lines of code. Any line maybe throw a NullPointerException. I don't want to care about these exceptions. If a line throws the exceptions, I want the executor jump to next line and go ...
4 votes
3 answers
601 views
What is a benefit of avoiding != null in java?
I have read this topic: Avoiding != null statements but I don't see benefits. If you return empty array, you need to examine where the "real" array is, and where the empty array is. Instead of using ...
1 vote
5 answers
7k views
Ruby: best way to handle multiple returns
I am working in Ruby and have found the need to have conditional return statements at the end of some/most of my methods. Here is what I have: # <ident-list> -> [ident] <ident-A> ...
66 votes
1 answer
4k views
Getting NullPointerException in Android SDK's method updateLocaleListFromAppContext
I have an app uploaded on PlayStore and getting crash on it java.lang.NullPointerException from android.app.ActivityThread.updateLocaleListFromAppContext I have tried searching a lot online to check ...
4 votes
2 answers
7k views
Should I avoid returning null in my java function? [closed]
I don't find the appropriate solution for my question. As far as I know returning null is a not a good way to write clean code, as the book Clean Code says. However there are many different opinions ...