Linked Questions
167 questions linked to/from How do I avoid checking for nulls in Java?
55 votes
6 answers
17k views
How to avoid checking for null values in method chaining? [duplicate]
I need to check if some value is null or not. And if its not null then just set some variable to true. There is no else statement here. I got too many condition checks like this. Is there any way to ...
21 votes
4 answers
14k views
Is there a shortcut to execute something only if its not null? [duplicate]
I find myself constantly writing this statement MyObject myObject = something.getThatObject(); if( myObject !=null && myObject .someBooleanFunction()){ } in order to prevent a null ...
6 votes
4 answers
3k views
What's the best way to avoid lots of "if (obj != null)" in Java code? [duplicate]
Possible Duplicate: How to avoid “!= null” statements in Java? Share your thoughts..
6 votes
2 answers
16k views
Set property only if is not null [duplicate]
I have some data in input that I'll have to use to set all properties of a POJO. The POJO might be partially set. My problem is to set the property only if related input data is not null. I know I can ...
17 votes
4 answers
7k views
How to avoid write code like "XXX!=null" or "XXX!=null || XXX.isEmpty" [duplicate]
I am writing codes that query the database to get the data. There are some classes that consist of Lists, but sometime the list or other attribute can not be initiated and their value is null, so I ...
-6 votes
6 answers
3k views
How avoid multiple IF loops in Java [duplicate]
What is the best way to avoid multiple if blocks which is used for null checks in Java? The following is my sample code. Which one is the most optimized way? if (address!=null} { if (firstName!=...
4 votes
4 answers
27k views
How to handle null pointer and what is "null safe" way to code? [duplicate]
I am a junior developer and I use java to do website development. I know org.apache.common.lang.StringUtils is recommended because of its null safty. But what is null safe or null safty exacty? why ...
0 votes
2 answers
21k views
Java: Best practice for null check [duplicate]
I know that NULL check for every arguments in a public API is good practice in java. But just curious, do we need do such NULL check in the front of all methods. There are definitely values for doing ...
-2 votes
4 answers
23k views
How to avoid null insertion in ArrayList? [duplicate]
How to avoid null insertion in ArrayList while inserting element? ArrayList<String> al=new ArrayList<String>(); al.add(null);//should avoid . . . al.add("Ramesh");
0 votes
6 answers
10k views
Calendar Null Object [duplicate]
public class Test { //required fields private String str1; private String str2; //optional private Calendar cal; public Test(String a, String b){ str1 = a; ...
12 votes
0 answers
6k views
android studio: java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter [duplicate]
I am trying to build a geofencing app using Kotlin in the android studio but the VM crashed then the next run show this error E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.mapgis, PID: ...
0 votes
1 answer
3k views
NullPointerException: attempt to invoke virtual method on a null object reference on setBackgroundColor [duplicate]
I'd like to to make possible that layout with options to change the background color of MainActivity. I don't know how to do this- how tell the program to change the background color of other view, in ...
6 votes
2 answers
2k views
NullPointerException in StaggeredGridLayoutManager [duplicate]
I'm getting some crash reports about a NullPointerException happening in StaggeredGridLayoutManager. Looks like around 10% of my users are suffering this issue. I have a very basic RecyclerView with ...
0 votes
1 answer
2k views
NullPointerException: Attempt to read from field 'java.lang.String com.salmanibrahim.firebasechat.model.User.name' on a null object reference [duplicate]
I have been trying lately but I just can't find where is the null object, help me understanding this error: 2021-01-24 01:10:47.244 24211-24211/com.salmanibrahim.firebasechat E/AndroidRuntime: FATAL ...
2 votes
2 answers
402 views
Optimistic way of checking null references in java [duplicate]
Can any Java proficient / expert suggest me what is the optimistic way of checking null reference or objects before using it to avoids NullPointerException? In my code I've more than 100 fields and ...