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.

11
  • 2
    Is this the whole code? Or there are commented lines? Commented Apr 16, 2015 at 19:32
  • 1
    Who is the "it" that is warning you? Commented Apr 16, 2015 at 19:33
  • 1
    @molbdnilo Eclipse's compiler Commented Apr 16, 2015 at 19:34
  • 2
    @user2150250 NetBeans doesn't show warning... so it seems more of a preference of Eclipse. Commented Apr 16, 2015 at 19:34
  • 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. Commented Apr 16, 2015 at 19:36

2 Answers 2

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
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

So I tried changing the switch to a series of if elses, leaving the event of tlsVersionMax being null in the else rather than one of the else ifs 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, 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 covers it in all details.
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.

1 Comment

It's by far not the only way. Placing if(baz == null) return; in front of the existing if would also cause the warning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.