Alex Rivera | Logout

Java entering an if statement that is false

Asked 2011-11-03T03:18:00.597
8

I'm running into the strangest error in this program, which is confirmed when debugging it. I have the following code (boiled down 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);
  }
}

Output

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 is extended and run from a child class that calls this constructor, but that really shouldn't affect this behavior. The problem is that, when false is passed to the constructor as useInternalStateSaver, the first if (uSS) is skipped, but the second is entered. Upon debugging, I found that uSS is false throughout runtime, including on the line of the second if statement, here

Edit
Report

1 Answer

0

I've seen crazy stuff like this when there is bad RAM on the machine. You might want to run memtest86.

You might also consider deleting all of your project class files, and then doing a build. Maybe you changed Main.java, but it was never recompiled. I hate that when that happens.

answered 2011-11-03T03:32:14.480

Your Answer