简体   繁体   English

Java输入错误的if语句

[英]Java entering an if statement that is false

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. BHFrame类是从调用此构造函数的子类扩展并运行的,但实际上不应影响此行为。 The problem is that, when false is passed to the constructor as useInternalStateSaver , the first if (uSS) is skipped, but the second is entered. 问题在于,当将false作为useInternalStateSaver传递给构造useInternalStateSaver ,将跳过第一个if (uSS) ,但输入第二个。 Upon debugging, I found that uSS is false throughout runtime, including on the line of the second if statement, here. 调试后,我发现在整个运行时, uSSfalse ,包括此处的第二条if语句行。 Why would Java enter an if statement when the condition returns false ? 当条件返回false时,Java为什么要输入if语句? Before you ask, I did delete the .class files and recompile it just in case there was some residual code messing with it, but I got the same result. 在您询问之前,我确实删除了.class文件并重新编译它,以防万一有一些残留的代码使它混乱,但是我得到了相同的结果。 And rest assured, all the references to the uSS variable are displayed here. 放心,这里显示了对uSS变量的所有引用。

Solution


As it turns out, this appears to be a bug in NetBeans 7.1 Build 201109252201, wherein the IDE doesn't properly insert new code into the compiled .class files. 事实证明,这似乎是NetBeans 7.1 Build 201109252201中的错误,其中IDE无法正确地将新代码插入已编译的.class文件中。 The problem was fixed by compiling the files externally. 通过外部编译文件解决了该问题。 A bug report has been submitted. 错误报告已提交。

Whatever's throwing that exception is probably not in your posted code. 无论抛出什么异常,都可能不在您发布的代码中。

It's not being caught by your catch statement, which only catches IOException. 您的catch语句不会捕获它,它只会捕获IOException。

It's a NullPointerException and can occur anywhere. 这是NullPointerException,可以在任何地方发生。

You have shown no indication that the code inside your if block is actually executing. 您没有迹象表明if块中的代码实际上正在执行。 In your screenshot, there is absolutely know way of knowing if your if block is entered or not. 在您的屏幕截图中,绝对有一种知道是否输入if块的方法。 There are no logging statements. 没有日志记录语句。

Add debugging messages at various points to see exactly what is happening. 在各个点添加调试消息以查看发生了什么。 Or, you know, look at line 26 (wayyyyy before your posted code) to see why you're getting a NullPointerException. 或者,您可以查看第26行(在您发布的代码之前,wayyyyy)以了解为什么会收到NullPointerException。

I've seen crazy stuff like this when there is bad RAM on the machine. 当机器内存不足时,我已经看到过类似的疯狂事情。 You might want to run memtest86. 您可能要运行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. 也许您更改了Main.java,但从未对其进行重新编译。 I hate that when that happens. 我讨厌那件事发生。

This is just a guess, because I can't see the code you are mentioning, but I reckon you have defined a local variable uSS in the second //More init code segment. 这只是一个猜测,因为我看不到您提到的代码,但是我认为您已经在第二个//More init code段中定义了局部变量uSS

Once you define a local variable named the same as an instance variable, it 'hides' the instance variable. 一旦定义了与实例变量相同的局部变量,它就会“隐藏”该实例变量。 Better to qualify all instance variables with this . 最好用this来限定所有实例变量。

So, try qualifying all above accesses of uSS with this. 因此,请尝试uSS限定对uSS所有上述访问this. ... ( this.uSS ) ...( this.uSS

Even if this isn't the issue, it might be better to post the full code anyway. 即使这不是问题,仍然最好张贴完整的代码。

HTH 高温超导

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM