views:

213

answers:

2

Hi I would like to know diff between the above comparisons?

I am getting null pointer exception when I check object.getItems() == null. But if I change it to null == object.getItems(), it workes fine.

I did look into this http://stackoverflow.com/questions/2938476/what-is-the-difference-between-null-object-and-objectnull-closed But I didnt get satisfactory answer.

+2  A: 

No, those two expressions are equivalent. Perhaps you're mixing it up with the fact that

nonNullObj.equals(obj)

and

obj.equals(nonNullObj)

can make a difference (since the second alternative could result in a NPE).

aioobe
what is the reason second expressoin NPE
Rozer
@Rozer, if obj is null, then the .equals method call would result in an NPE.
aioobe
A: 

in

object.getItems() == null

I believe that the code will get the items value, then check if it equals null

in

null == object.getItems()

First the app will detect the null value, then get the items to compare it to.

rockit
Ehm, did you try it yourself?
BalusC
Both cases will do exactly the same thing. Try it if you like. Both will throw NPE if object is null, otherwise return true if and only if object.getItems() returns null.
mikera