Java introduces an if statement, which is false

I run the strangest error in this program, which is confirmed when debugging it. I have the following code (compiled to highlight the problem, of course):

BHFrame.java

 public class BHFrame { private boolean uSS; private StateSaver stateSaver; public BHFrame(boolean useInternalStateSaver) { //Init code uSS = useInternalStateSaver; //More init code System.out.println(uSS); if (uSS) {System.out.println("Entered 1"); stateSaver = new StateSaver(title, false); stateSaver.addSaveable(getThis()); } //More init code System.out.println(uSS); if (uSS) {System.out.println("Entered 2"); try { stateSaver.loadState(); stateSaver.putState(getThis()); } catch (IOException ex) { alertUserOfException(ex); } } } } 

GUI.java

 public class GUI extends BHFrame { public GUI(boolean useInternalStateSaver) { super(useInternalStateSaver); } } 

Main.java

 public class Main { public static void main(String[] args) { GUI gui = new GUI(false); } } 

Exit

 false false Entered 2 Exception in thread "main" java.lang.NullPointerException at bht.tools.comps.BHFrame.<init>(BHFrame.java:26) at bhms.GUI.<init>(GUI.java:5) at bhms.Main.main(Main.java:5) 

The BHFrame class extends and runs from a child class that calls this constructor, but this really should not affect this behavior. The problem is that when false is passed to the constructor as useInternalStateSaver , the first if (uSS) skipped and the second is entered. After debugging, I found that uSS false at run time, including in the line of the second if . Why does Java introduce an if when the condition returns false ? . Before you ask, I deleted the .class files and recompiled it only if there was some residual code processing with it, but I got the same result. And be sure all references to the uSS variable uSS displayed here.

Decision


As it turned out, this looks like a bug in NetBeans 7.1 Build 201109252201, in which the IDE incorrectly inserts new code into compiled .class files. The problem was fixed by compiling the files from the outside. A bug report was sent.

+7
source share
3 answers

Whatever you choose for this exception is probably not in your published code.

It does not fall into your catch statement, which only catches an IOException.

This is a NullPointerException and can occur anywhere.

You did not indicate that the code inside your if block if actually executing. In the screenshot there is an absolutely famous way to find out if your if block is entered or not. There are no registration messages.

Add debugging messages at different points to see exactly what is happening. Or, you know, look at line 26 (wayyyyy before your published code) to find out why you get a NullPointerException.

+1
source

This is just an assumption because I don’t see the code you mention, but I suppose you defined the local uSS variable in the second segment //More init code .

Once you define a local variable with a name similar to an instance variable, it β€œhides” the instance variable. It is better to qualify all instance variables with this .

So, try to assign all of the above uSS with this. ... ( this.uSS )

Even if this is not a problem, in any case it is better to place the full code.

NTN

0
source

I saw such crazy things when the machine has bad RAM. You might want to run memtest86.

You can also consider deleting all project class files and then creating an assembly. You may have changed Main.java, but it never recompiled. I hate it when this happens.

-one
source

All Articles