2010-08-20 27 views
0

我試圖捕捉異常,然後在JTextArea顯示,但我得到空...爪哇 - 的getMessage()返回null

這就是:

} catch (Exception rwe) { 
        // System.exit(0); 
        game.remove(yPanel); 
        game.remove(xPanel); 
        game.remove(roomPanel); 
        game.remove(userPanel); 
        game.remove(titlePanel); 
        game.remove(introPanel); 
        remove(game); 
        remove(sp); 
        remove(invOne); 
        remove(main); 
        remove(say); 
        add(statusPanel); 
        JTextArea errorText = new JTextArea(); 
        errorText.append("Here is the reason why it crashed:\n" +rwe.getMessage()); 
        errorText.setPreferredSize(new Dimension(500,300)); 
        System.out.println("errorrr:" + rwe.getMessage()); 
        statusPanel.add(errorText); 

      statusPanel.setOpaque(true); 
      labelStatus.setVisible(true); 
        System.out.println("Server crashed"); 
        c.append("\nServer crashed..."); 
        rwe.printStackTrace(); 
      } 

與當發生錯誤我在JTextArea裏面得到這個:

Here is the reason why it crashed: 
null 

爲什麼?

回答

9

因爲例外rwe不包含消息。例如,這通常是NullPointerException的情況。除了消息外(如果有),您可能還想包括該異常類型。

1

您正試圖在JTextarea中顯示異常消息。例外情況不需要有消息。也許這不是。