Why do I get a "dead code" warning in this Java code?
Asked Answered
C

3

5

Would someone please tell me why i am getting a dead code warning in the else branch of if (projectId != null) ? If i got this right, the interpreter thinks projectId can never be null - is that right? In my opinion this is not possible...

Integer projectId = null;

if (!sprintTaskConnections.isEmpty())
    projectId = sprintTaskConnections.get(0).getProjectId();

// init name, state, startDate, endDate here

JiraSprint sprint = new JiraSprint(sprintInfo.getInt("id"), name, state, projectId, startDate, endDate);

if (projectId != null)
{
   ...
}

Even if i put a

sprintTaskConnections.add(new JiraSprintTaskConnection(1, 1, 1));

or a

sprintTaskConnections.clear();

in front of

if (!sprintTaskConnections.isEmpty())
projectId = sprintTaskConnections.get(0).getProjectId();

the result is always the same!

Please help me, i just don't get it at the moment!

Canoewood answered 17/9, 2013 at 12:36 Comment(6)
Who is giving this warning? What interpreter do you mean?Hindsight
You forgot to turn on the halting problem solver.Fiendish
Is it just a warning? The checks you are doing seem to be a smart design, so you could possibly just ignore the warning...but I'd need more detail on "if (!sprintTaskConnections.isEmpty())"Unexampled
@Canoewood But what IDE / compiler are you using?Serviceberry
Maybe unrelated question, but why bother getting all the config information is the sprintTaskConnections is empty? You would know up front that its just going to fail throw a few lines later anyways...Fiendish
@Dukeling: I am using the newest version of eclipse.Canoewood
C
11

I don't have the JiraSprint code, so I can't confirm this, but I suspect the JiraSprint constructor takes an int where you pass in the projectId rather than an Integer. That forces Java to auto-unbox the Integer. If the Integer projectId is null, you'll get a NullPointerException (because of the auto-unboxing) there, so you won't even get to the if block. Therefore, projectId must not be null.

Cobbs answered 17/9, 2013 at 12:44 Comment(2)
This is much more likely actually. +1.Heteronomous
You're my hero for the day! Thank you!Canoewood
M
1

Maybe: new JiraSprint(..) gets int projectId?

In that case, if projectId is null, this line:

JiraSprint sprint = new JiraSprint(sprintInfo.getInt("id"), name, state, projectId, startDate, endDate);

will throw NPE, so projectId cannot be null after this line.

Martella answered 17/9, 2013 at 12:48 Comment(0)
C
-3

projectId has a high chance that it will always be null so the code in the "if" will never get executed

Christiansand answered 17/9, 2013 at 12:39 Comment(5)
But it's assigned here - projectId = sprintTaskConnections.get(0).getProjectId();.Serviceberry
only if !sprintTaskConnections.isEmpty()Christiansand
No, if sprintTaskConnections is not empty, projectId will get a value assigned.Canoewood
The dead code is the code inside the else part. Aka the compiler thinks projectId will NEVER be nullBesought
You are correct, i was completly missing the point there, TheTerribleSwiftTomato alreay pointed out the not-so-obviousChristiansand

© 2022 - 2024 — McMap. All rights reserved.