1

This question is not related to any problem in particular. Just trying to know what's going on.

I have the following statement:

Integer myInt = null;
if(myInt!=null) {
  // it's not entering here...
}

The execution flow normally, and the lines within 'if' statement are correctly not executed. However when highlighting and inspecting (ctrl+shift+i) myInt!=null instead of getting false I got the error within inspector stating:

myInt.intValue() null pointer exception

I wonder why Eclipse need a convertion to int.

UPDATE

I have corrected the sentence, stating that the inspector should return false. The whole point of question is why Eclipse is calling intValue within the inspector, and not a discussion about int or Integer.

Leonardo
  • 9,607
  • 17
  • 49
  • 89
  • myInt!=null should return false, not true, because it's null. And it's nonsense it tries to get int value because an int can't ever be null. Btw, it doesn't happen in my Eclipse, it yields 'false' – Amin Abu-Taleb Sep 04 '13 at 10:34
  • maybe eclipse calls that method by default because there are 2 options: the element is null (and any if statement or comparation will fail) or the element is a number. so it thinks there's no need to worry, an int can't be null so eclipse is trying to get its value in any case – Gianmarco Sep 04 '13 at 10:34
  • 1
    Ok first of all, `myInt!=null` will never give you `true`. It's always false since you have assigned `myInt=null` – Aneesh Sep 04 '13 at 10:36
  • Also have you defined `myInt` as `Integer` or `int` in your code? – Aneesh Sep 04 '13 at 10:38
  • Better have a look in to this [thread][1]. [1]: http://stackoverflow.com/questions/2254435/can-an-int-be-null-in-java – Satheeq Sep 04 '13 at 10:39
  • @Amin which version of Eclipse you have ? – Leonardo Sep 04 '13 at 10:52
  • @Leonardo Eclipse Helios, and yours? – Amin Abu-Taleb Sep 04 '13 at 10:56
  • @Leonardo You should not change your basic question here first of all your priority was why it is not returning `true` and why it is not entering in if condition. – commit Sep 04 '13 at 11:00
  • I didn't change the original question. As you can see the title is left unchanged and it explain it all. – Leonardo Sep 04 '13 at 11:47

2 Answers2

0

By inspecting the Integer object eclipse tries to call the

intValue()

function of the Integer class, which return the integer value of the class, beacuse java want to compare the Integer in the same way it compares an int.

example:

 int i = 5;
 if (i == 6){
    //Will never enter here
 }

will give you the same result as

 Integer i = new Integer(5);
 if (i == 6) {
    //Will never enter here
 }

Beacuse java will do the autounboxing for you.

If the Integer is null the function intValue() called to convert from the Integer i to the number 5 for camparison throws a null pointer exception

chiarfe
  • 522
  • 3
  • 15
0

I ran the exact same code and it ran just fine without any null pointer exception. Maybe there is some other issue somewhere else in the code.

Aneesh
  • 1,703
  • 3
  • 24
  • 34