Findbugs给出了“System.out的空指针取消引用”,为什么?

时间:2011-07-06 16:07:25

标签: java eclipse findbugs java-7

我正在使用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是对的,但是现在消息发生了什么改变了吗?

3 个答案:

答案 0 :(得分:15)

因为在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,因此它给出的错误消息毫无意义。

答案 1 :(得分:11)

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

答案 2 :(得分:3)

这只发生在openjdk上,而不是sun jdk。

问题是2010年发布的一个补丁,允许系统时间早于1970年。

http://mail.openjdk.java.net/pipermail/distro-pkg-dev/2010-July/009869.html