0
public void addValues(final Set<String> values) { if (values != null || !values.isEmpty()) { //compiler warning here on values.isEmpty(). Null pointer access: The variable values can only be null at this location } } 

Why am I seeing this NullPointerAccess warning, though I am guarding against the null in the first check. If the value is null first check would fail and it will return without even going to the second check.

Can I ignore this warning.

I am using Eclipse for development.

4 Answers 4

4

If values is null, values != null will be evaluated to false and then !values.isEmpty() will be evaluated, leading to a NPE.

You probably meant

if (values != null && !values.isEmpty())

Can I ignore this warning.

Don't ignore warnings, they are there for something.

Sign up to request clarification or add additional context in comments.

Comments

1

Why am I seeing this NullPointerAccess warning, though I am guarding against the null in the first check.

No you're not. If values is null, the first condition is false; therefore the second condition is evaluated, this is how the logical or operator works.

You probably meant to use && instead.

Comments

0

You may think you're guarding against null, but you're not. If values is null, then values!=null will be false (so the next condition will need to be evaluated) and then !values.isEmpty() will raise a NullPointerException.

What you seem to mean is:

if (values!=null && !values.isEmpty()) { ... 

Comments

0

If values is null, the code will still try to evaluate values.isEmpty(). Use the && operation instead:

if(values != null && !values.isEmpty()){ // blah... } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.