Java input if statement is false
I encountered the strangest error in this program and will be confirmed when debugging it I have the following code (which of course boils down to outstanding problems):
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); } }
yield
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 class bhframe extends and runs from a subclass that calls this constructor, but should not actually affect this behavior The problem is that when false is passed to the constructor as useinternalstatesaver, skip the first if (USS), but enter the second if During debugging, I found that USS was wrong throughout the run time, including on the line of the second if statement, here Why does Java enter an IF statement when the condition returns false? I did delete it before you asked Class file and recompile it in case some residual code messes it up, but I got the same result Rest assured that all references to the USS variable are displayed here
solution
It turns out that this seems to be an error in NetBeans 7.1 build 201109252201, where the IDE does not correctly insert new code into the compiled Class file This problem is solved by external compiled files Submitted bug report
Solution
No matter what throws the exception, it may not be in your published code
It is not captured by the catch statement, it only captures IOException
This is a NullPointerException that can occur anywhere
You have no indication that the code in the if block is actually executing In the screenshot, you absolutely know how to enter the if block No logging
Add debug messages at various points to see exactly what happened Or, you know, look at line 26 (wayyy before you release the code) and see why you get a NullPointerException