1

I have a method that takes an Integer baz as an optional parameter via overloading like so ...

public static void myMethod(String fooBar, Integer baz) {

    switch(baz){} // baz gets unboxed, NPE if it was null

    if(baz != null) {
        // Do something
    }
    else {
        // Do something else
    }
}

public static void myMethod(String fooBar) {
    myMethod(fooBar, null);
}

However, it's giving me a warning about the else statement saying it's dead code. But when the method is called without baz, baz should just default to null so this should not be dead code. Any explanation as to what I'm doing wrong here? How would baz always be non-null even though I set it to null in the overloaded method?

EDIT: Deleted actual code so my question is easier to read.

user2150250
  • 4,797
  • 11
  • 36
  • 44
  • 2
    Is this the whole code? Or there are commented lines? – M A Apr 16 '15 at 19:32
  • 1
    Who is the "it" that is warning you? – molbdnilo Apr 16 '15 at 19:33
  • 1
    @molbdnilo Eclipse's compiler – user2150250 Apr 16 '15 at 19:34
  • 2
    @user2150250 NetBeans doesn't show warning... so it seems more of a preference of Eclipse. – gtgaxiola Apr 16 '15 at 19:34
  • @manouti There's code in place of the `Do somethings` but nothing that should affect this warning – user2150250 Apr 16 '15 at 19:36
  • 3
    This does not throw any warning/errors in Eclipse - you must have some other code that is setting the value of `baz` in the method to something other then null. Please post additional context. – Matt Clark Apr 16 '15 at 19:36
  • Eclipse (Luna) doesn't show any warnings for this code. Did you disable the `Build automatically` option? If yes, did you rebuild the project after some changes? – Alex Shesterov Apr 16 '15 at 19:36
  • One moment, it seems necessary for me to post the remainder of the code so I'll do so ... – user2150250 Apr 16 '15 at 19:38
  • Also, if you modify `baz` before `if`, e.g. `baz = 0;`, then Eclipse does show the warning. – Alex Shesterov Apr 16 '15 at 19:39
  • @AlexShesterov Yeah if I default `baz` to `-1` instead of `null` and process it that way it doesn't give any warnings and works just fine but I wanted to know why I was getting the warning for default of `null` – user2150250 Apr 16 '15 at 19:40
  • 1
    Sorry, I wasn't clear. I meant, the code snippet you've posted doesn't produce any warnings, but some statement in `// code here` could cause the warning. Please post your whole method. – Alex Shesterov Apr 16 '15 at 19:43

2 Answers2

4

You are using switch on the Integer variable. If the Integer is null, switch throws a NullPointerException, which you don't catch.

So it's not possible for the program flow to reach the else condition.

Integers are unboxed if used in switch statement. You have to check for null explicitly, before the switch statement. See also How to use null in switch.


P.S. the minimal code to reproduce the problem is:

public static void myMethod(String fooBar, Integer baz) {

    switch(baz){} // baz gets unboxed, NPE if it was null

    if(baz != null) {
        // Do something
    }
    else {
        // Do something else
    }
}
Community
  • 1
  • 1
Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
  • So I tried changing the `switch` to a series of `if else`s, leaving the event of `tlsVersionMax` being `null` in the `else` rather than one of the `else if`s and that threw a null pointer exception when I ran my code. Then I tried framing it in an `if(tlsVersionMax != null)` like is suggested in the link you posted and it worked. Why can't the event of the variable being null just be covered by the `else`? – user2150250 Apr 16 '15 at 20:16
  • 1
    @user2150250, comparing an `Integer` with an `int` throws an NPE in Java if the `Integer` reference is `null`. This is because `Integer` is unboxed for value comparison operation. [This question](http://stackoverflow.com/q/3352791/2170192) covers it in all details. – Alex Shesterov Apr 16 '15 at 20:30
1

The only way that Eclipse tells you that your else statement is dead code is that you assign a new Integer to your baz before the if statement, therefore making it impossible to be null.

But since you're only showing a part of your code, that's hard to tell.

EDIT

The switch statement will throw a NullPointerException if tlsVersionMax is null. That's why you else statement is dead code.

Neumann
  • 551
  • 2
  • 6
  • 17
  • It's _by far_ **not** the only way. Placing `if(baz == null) return;` in front of the existing `if` would also cause the warning. – Alex Shesterov Apr 16 '15 at 19:51