user3131471
user3131471

Reputation: 63

Null pointer Exception in array List

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.

Upvotes: 0

Views: 606

Answers (8)

Jamel ESSOUSSI
Jamel ESSOUSSI

Reputation: 200

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
    }
}

Upvotes: 2

Aniket Kulkarni
Aniket Kulkarni

Reputation: 12993

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.

Upvotes: 0

venergiac
venergiac

Reputation: 7727

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 &&

Upvotes: 1

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35587

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.

Upvotes: 1

Buddhima Gamlath
Buddhima Gamlath

Reputation: 2328

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()

Upvotes: 1

Maroun
Maroun

Reputation: 96016

& 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.

Upvotes: 3

stinepike
stinepike

Reputation: 54742

use && instead of &

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

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

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.

Upvotes: 0

Related Questions