This question already has an answer here:
- Null check chain vs catching NullPointerException 19 answers
- Avoiding != null statements 61 answers
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 handle this null checks without checking all method return values?
if(country != null && country.getCity() != null && country.getCity().getSchool() != null && country.getCity().getSchool().getStudent() != null .....) {
isValid = true;
}
I thought directly checking variable and ignore NullpointerException. Is this a good practice?
try{
if(country.getCity().getSchool().getStudent().getInfo().... != null)
} catch(NullPointerException ex){
//dont do anything.
}
You could also look at vavr's Option which as the below posts describes is better than Java's Optional and has a much richer API.
https://softwaremill.com/do-we-have-better-option-here/
The object-oriented approach is to put the isValid method in Country and the other classes. It does not reduce the amount of null checks, but each method only has one and you don't repeat them.
This has the assumption that validation is the same everywhere your Country is used, but typically that is the case. If not, the method should be named hasStudent(), but this is less general and you run the risk of duplicating the whole School interface in Country. For example, in another place you may need hasTeacher() or hasCourse().
Another approach is to use null objects:
I'm not sure it is preferable is this case (strictly you would need a sub class to override all modification methods), the Java 8 way would be to go with Optional as method in the other answers, but I would suggest to embrace it more fully:
Both for null objects and Nullable only work if you always use them instead of null (notice the field initialization), otherwise you still need the null checks. So this option avoid null, but you code becomes more verbose to reduced null checks in other places.
Of course, the correct design may be to use Collections where possible (instead of Optional). A Country has a set of City, City has a set of Schools, which has set of students, etc.
As alternative to other fine usage of
Optional
, we could also use a utility method with aSupplier<Object>
var-args as parameter.It makes sense as we don't have many nested levels in the object to check but many fields to check.
Besides, it may easily be modified to log/handle something as a
null
is detected.Suppose you would like to add some traces, you could so write :
Java doesn't have "null-safe" operations, like, for example Kotlin's null safety
You can either:
Otherwise if you have control over the domain objects, you can redesign your classes so that the information you need is available from the top level object (make
Country
class do all the null checking...)No, it is generally not good practice in Java to catch a NPE instead of null-checking your references.
You can use
Optional
for this kind of thing if you prefer:or simply
if that is all that
isValid
is supposed to be checking.You could use
Optional
here, but it creates one Optional object at each step.