0
ArrayList<String> list1=new ArrayList<String>();
list1=object.getsomeFiles();

getsomeFiles() contains some code which returns file names if exists or returns null

I checked with

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

but still NullPointerException is thrown.

When I did debugging I found that list1 holds null value.

1
  • 2
    After edit still NPE then your object is null. Check that you have valid reference in object on which you are calling getsomeFiles(). Commented Jan 2, 2014 at 8:33

8 Answers 8

3

& will break the Short-circuit evaluation, use && instead.


Explanation:

If you have:

if(a() & b())

Then both a and b will be performed, and the final result will be evaluated.

But if you have:

if(a() && b())

Then if a() is false, b() won't be reached.

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

Comments

2

The "object" instance is null.

ArrayList<String> list1=new ArrayList<String>();
if(Object!=null){
    list1=object.getsomeFiles();
    if(list1!=null&&!list1.isEmpty()){
        // Your code here
    }
}

Comments

1

The problem is, if you used & to check two conditions, both statements get evaluated regardless of first statement being true or false. If first one is false, the evaluating the second one results in a NullPointerException. To overcome this, you should you the && operator. It will evaluate the second expression only if first is true, that is only if list1 is not null.

Therefore, the correct condition inside the if should be

list1 != null && !list1.isEmpty()

Comments

1

Change if(list1!=null&!list1.isEmpty()) in to if(list1!=null&&!list1.isEmpty())

short-circuit(like &&), don't evaluate the right hand side if it that doesn't necessary. As an example if && left hand side is false no need to evaluate right hand side one. In other way || if left is true no need to evaluate right hand side one. So if list is null rest of the part will not evaluate.

non-short(like &) evaluvate both side always.So you will get NullPointerException.

Comments

1

As answered by the other guys change & to &&.

The problem is due to the difference between & and &&.

eg.

 if (A==B && B==C) 

first jvm evaluates A==B if false exit with false

 if (A==B & B==C) 

first JVM evaluates A==B if false or true JVM evaluates also B==C

it is a Java specification called "short-circuited"

See Difference between & and &&

Comments

0

All the answers explained about & and && very well.

But, after you edit your are still getting NullPointerException then problem is your object is null on which you are calling getsomeFiles() method.

list1=object.getsomeFiles();
       ↑ //null  

In you code if you have

SomeFileClass object; //here object is null

Make sure that object is initialized like:

object = new SomeFileClass();

Or initialize at object creation

SomeFileClass object = new SomeFileClass();

From Java docs NullPointerException

Thrown when an application attempts to use null in a case where an object is required. These include:

  • Calling the instance method of a null object.
  • Accessing or modifying the field of a null object.
  • Taking the length of null as if it were an array.
  • Accessing or modifying the slots of null as if it were an array.
  • Throwing null as if it were a Throwable value.

Comments

0

Replace single & with double && in your if condition

This

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

should be

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

&& is logical and i.e. true && true is true and everything else is false. whereas & is bitwise and.

Comments

0

use && instead of &

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.