How to avoid checking for null values in method chaining?

user9521067 :

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.
}
khelwood :

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:

if (Optional.ofNullable(country)
            .map(Country::getCity)
            .map(City::getSchool)
            .map(School::getStudent)
            .isPresent()) {
    isValid = true;
}

or simply

boolean isValid = Optional.ofNullable(country)
                          .map(Country::getCity)
                          .map(City::getSchool)
                          .map(School::getStudent)
                          .isPresent();

if that is all that isValid is supposed to be checking.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to avoid checking for null values in method chaining?

How to avoid redundant method calls when checking the result first for null?

How to avoid null checking in Java?

JQuery how to avoid next() method chaining

How to avoid null warning when using @NotNull and checking for null in another method before method call?

How could I avoid == null checking?

How to avoid null checking before foreach IList

How to Avoid Copies When Doing Method Chaining in C++

How to avoid null values ​in a stream

How to avoid multiple if statements checking if property is null in typescript

How to Implement Method Chaining

How to implement this method chaining

How to avoid null in Java method chain

How to avoid overwriting of non-null values with null values?

How to extract null check into method and get valid null type checking?

Checking for NULL values in SQL

checking null values in a dataframe

Checking db for null values

How to assign a value to sqlparameter NULL values without checking for null or 0?

Itertuples - avoid Null values

How to write unit testing checking null input values using wicket

Can we add optional chaining in .classlist for null checking in jquery?

How to replace values in a subset of data, based on values of other columns, within a Pandas method chaining expression

Conditionally chaining promises - how to avoid code duplication?

How does method chaining work?

How to implement method chaining with inheritance?

How to achieve method chaining in Java?

Avoid duplication of null type checking in Dart

pandas: assign a column values by slice with method chaining