Avoiding null statements

0 votes

I use object != null a lot to avoid NullPointerException.

Is there a good alternative to this?

For example:

if (someobject != null) {
    someobject.doCalc();
}

This avoids a NullPointerException, when it is unknown if the object is null or not.

Note that the accepted answer may be out of date, see https://stackoverflow.com/a/2386013/12943for a more recent approach.

May 22, 2018 in Java by Parth
• 4,630 points
476 views

2 answers to this question.

0 votes

Depending on what kind of objects you are checking you may be able to use some of the classes in the apache commons such as: apache commons lang and apache commons collections

Example:

String foo;
...
if( StringUtils.isBlank( foo ) ) {
   ///do something
}

or (depending on what you need to check):

String foo;
...
if( StringUtils.isEmpty( foo ) ) {
   ///do something
}

The StringUtils class is only one of many; there are quite a few good classes in the commons that do null safe manipulation.

Here follows an example of how you can use null vallidation in JAVA when you include apache library(commons-lang-2.4.jar)

public DOCUMENT read(String xml, ValidationEventHandler validationEventHandler) {
    Validate.notNull(validationEventHandler,"ValidationHandler not Injected");
    return read(new StringReader(xml), true, validationEventHandler);
}

And if you are using Spring, Spring also has the same functionality in its package, see library(spring-2.4.6.jar)

Example on how to use this static classf from spring(org.springframework.util.Assert)

Assert.notNull(validationEventHandler,"ValidationHandler not Injected");
answered May 22, 2018 by Rishabh
• 3,620 points
0 votes
public String getPostcode(Person person) {  
  return person?.getAddress()?.getPostcode();  
}  

The ?. means only de-reference the left identifier if it is not null, otherwise evaluate the remainder of the expression as null.

answered Aug 20, 2018 by Daisy
• 8,120 points

Related Questions In Java

0 votes
1 answer

Overloaded method for null in Java

The method invoked here will be the ...READ MORE

answered May 23, 2018 in Java by code.reaper12
• 3,500 points
748 views
0 votes
1 answer

How can we add JTable in JPanel with null layout?

The JPanel should have some layout manager. JTable ...READ MORE

answered Jun 1, 2018 in Java by Parth
• 4,630 points
1,221 views
0 votes
1 answer

Autowired field pointing NULL in Spring

The field annotated @Autowired is null because ...READ MORE

answered Jun 6, 2018 in Java by Avi
• 160 points

edited Mar 4, 2022 by Sarfaraz 14,211 views
0 votes
1 answer

What are preparedstatements? How do they differ from statements?

Statement is used for static queries like ...READ MORE

answered Jun 8, 2018 in Java by Daisy
• 8,120 points
532 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 79,494 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
7,952 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
16,986 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points

edited Aug 4, 2023 by Khan Sarfaraz 4,383 views
0 votes
1 answer

I am learning looping statements. Can you tell me how 'for-each' works in Java?

While programming we often write code that ...READ MORE

answered Apr 17, 2018 in Java by Rishabh
• 3,620 points
673 views
0 votes
2 answers

How does '&&' (AND) and '||' (OR) statements work inside an IF condition ?

Short circuit here means that the second ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,910 points
637 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP