2011-07-06 47 views
18

我正在使用Java 1.7,Eclipse 3.7和來自市場的FindBugs插件。這個例子是像你一樣的天堂:Findbugs給出了「System.out的空指針解引用」,爲什麼?

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

此消息是不存在的過去和內部實現總是在系統:

public final static PrintStream out = null; 

所以FindBugs的是對的,但做了一些變化使消息現在發生?

+1

不一定,有可能是一個塊的地方,被分配'out'的東西(一個'static'塊)。基於文檔,FindBugs仍然是實驗性的,可能無法正常工作。這聽起來像findbugs中的一個錯誤...如果你通過標準的JVM運行它,Ii會假設代碼有效? –

+2

當然可以。我認爲這與切換到Java 7有關,因爲初始化不再是靜態的(我認爲它是,我現在沒有Java 6版本)。 –

回答

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管理出本地代碼中的錯誤信息,因此它給出的錯誤信息是毫無意義的。

11

這在Findbugs 1.3.9中被標記爲bug。它已經被Findbugs 2.0修復,並可能被回溯。

+1

不幸的是,它仍在發生。至少我仍然通過Maven的Findbugs插件得到這個錯誤。在2.3.3版本中,應該使用Findbugs 2。 –

+0

在這種情況下,可能值得再次報告錯誤,評論清楚地表明它應該在findbugs中修復2。 – Thirler

相關問題