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.

user3131471
  • 71
  • 1
  • 6
  • 2
    After edit still `NPE` then your `object` is `null`. Check that you have valid reference in `object` on which you are calling `getsomeFiles()`. – Aniket Kulkarni Jan 02 '14 at 08:33

8 Answers8

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.

Maroun
  • 94,125
  • 30
  • 188
  • 241
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
    }
}
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.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
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()
Buddhima Gamlath
  • 2,318
  • 1
  • 16
  • 26
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 &&

Community
  • 1
  • 1
venergiac
  • 7,469
  • 2
  • 48
  • 70
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.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

use && instead of &

if(list1!=null && !list1.isEmpty())
stinepike
  • 54,068
  • 14
  • 92
  • 112
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.
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90