Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

dead-code

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!

like image 245
nidhoeggr09 Avatar asked Sep 17 '13 12:09

nidhoeggr09


2 Answers

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.

like image 191
Bron Avatar answered Nov 10 '22 20:11

Bron


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.

like image 40
BobTheBuilder Avatar answered Nov 10 '22 19:11

BobTheBuilder