Findbugs 给出了 “Null pointer dereference of System.out”,为什么?

2022-09-02 20:59:24

我正在使用Java 1.7,Eclipse 3.7和市场上的FindBugs插件。这个例子和天堂一样好:

class Application
{
  public static void main( String[] args )
  {
    System.out.println( "Bla" );
  }
}

此消息在过去不存在,内部实现始终在系统中:

public final static PrintStream out = null;

所以Findbugs是对的,但是现在发生了什么变化吗?


答案 1

因为在java 6中它看起来像这样:

public final static PrintStream out = nullPrintStream();

/**
 * The following two methods exist because in, out, and err must be
 * initialized to null.  The compiler, however, cannot be permitted to
 * inline access to them, since they are later set to more sensible values
 * by initializeSystemClass().
 */
private static PrintStream nullPrintStream() throws NullPointerException {
    if (currentTimeMillis() > 0) {
        return null;
    }
    throw new NullPointerException();
}

所以我想他们在java 7中简化了它,并为编译器添加了一些例外。

JVM 在本机代码中管理 out、in 和 err,因此它给出的错误消息毫无意义。


答案 2

这在 Findbugs 1.3.9 中被标记为 bug。它已针对 Findbugs 2.0 进行了修复,可能会被向后移植。


推荐