#10361 Code parsing error

I just updated to 10361 and now have a 'variable 'xxx' might not have been initialized' error in Java code that hasn't changed. The code compiles without errors. I have simplified it here:

boolean a = true;
boolean b = true;
boolean c = true;
String foo = "foo";
String bar = "bar";
String variable;   // <--- the variable in question
if (a && b && c) {   // <--- the expression that causes the problem
    variable = foo;
}
else {
    variable = bar;
}
String spong = variable; //  <-- Variable 'variable' might not have been initialized


The value of the booleans a, b, and c makes no difference, but if the expression is reduced to a single comparison, e,g, (a && b), or nested/grouped, e.g. (a && (b && c)) the problem goes away.

2 comments
Comment actions Permalink

I'm seeing the exact same thing, but it was with some new code I'd just written
so I wasted a few mins scratching my head trying to figure out what I'd done
wrong! My code compiles fine.

Here's a code snippet, looks like I have the same problematic if statement
as your example:

String server = ...
Type type;
int portIndex = server.indexOf(':');
int dbIndex = portIndex == 0 ? 0 : server.indexOf('/', portIndex + 2);
if (portIndex > 0 && dbIndex > 0 && dbIndex < server.length() - 1)
{
type = Type.Sybase;
...
}
else
{
type = Type.Oracle;
...
}

// At this point IDEA thinks 'type' may not have been initialised.


I just updated to 10361 and now have a 'variable 'xxx' might not have
been initialized' error in Java code that hasn't changed. The code
compiles without errors. I have simplified it here:

 boolean b = true;
> boolean c = true;
> String foo = "foo";
> String bar = "bar";
> String variable;   //  if (a && b && c) {   //  variable = foo;
> }
> else {
> variable = bar;
> }
> String spong = variable; //   been initialized]]>


The value of the booleans a, b, and c makes no difference, but if the
expression is reduced to a single comparison, e,g, (a && b), or
nested/grouped, e.g. (a && (b && c)) the problem goes away.

---
Original message URL:
http://www.jetbrains.net/devnet/message/5240583#5240583



0
Comment actions Permalink

Yes, that looks like the same thing. You can make it go away by putting redundant parentheses around the last && comparison in the expression. That seems to me the safest & easiest workaround. The alternatives are too intrusive (e.g. use conditional operator, or pre-evaluate the expression).

0

Please sign in to leave a comment.