我在这个程序中遇到了最奇怪的错误,在调试它时得到了确认。我有以下代码(归根结底是为了突出问题,当然):
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);
}
}
输出
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)
该类BHFrame
是从调用此构造函数的子类扩展和运行的,但这实际上不应该影响此行为。问题是,当false
作为 传递给构造函数时useInternalStateSaver
,第一个if (uSS)
被跳过,但第二个被输入。调试后,我发现它uSS
在false
整个运行时,包括在第二条if
语句的行,here。为什么条件返回时Java会输入if
语句false
?在你问之前,我确实删除了.class
文件并重新编译它,以防有一些残留的代码弄乱它,但我得到了相同的结果。请放心,对uSS
变量的所有引用都显示在此处。
解决方案
事实证明,这似乎是 NetBeans 7.1 Build 201109252201 中的一个错误,其中 IDE 没有正确地将新代码插入到编译.class
文件中。该问题已通过在外部编译文件得到解决。已提交错误报告。