2012-09-28 97 views
1

我遇到問題,爲什麼我要拋出一個nullpointerexception。它並沒有真正影響任何東西(至少我不這麼認爲),因爲所有東西都被正確地繪製出來了,但我不喜歡錯誤消息。如你所見,paint()調用this.game.paint(g2)。要嘗試調試,this.game.print中的所有內容都已被註釋掉 - 該函數不會執行任何操作,因此它不應該成爲問題的一部分。JFrame上的NullPointerException paint()

public class Application extends JFrame { 

private Game game;///<stores the rules for the game and facilitates interactions 

public Application() throws RoyException { 
    this.game = new Game(this, 2); 
} 


// @Override 
public void paint(Graphics g) { 
    Graphics2D g2 = (Graphics2D) g; 
    try{ 
    this.game.paint(g2); 
    }catch(Exception e) 
    { 
     System.out.println("CAUGHT " + e.getMessage()); 
     e.printStackTrace(); 
    } 
} 

public static void main(String[] args) { 
    try { 
     Application app = new Application(); 
    } catch (RoyException e) { 
     System.out.println("An error occurred while running the program: " + e.getMessage()); 
     System.out.print("Stack trace: "); 
     e.getStackTrace(); 
    } 
} 

}

而這裏的堆棧跟蹤 - Application.java:153是this.game.paint(G2);

CAUGHT null java.lang.NullPointerException 
    at bullshitakemushrooms.Application.paint(Application.java:153) 
    Game created. 
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:781) 
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728) 
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677) 
    at javax.swing.RepaintManager.access$700(RepaintManager.java:59) 
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621) 
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) 
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:701) 
    at java.awt.EventQueue.access$000(EventQueue.java:102) 
    at java.awt.EventQueue$3.run(EventQueue.java:662) 
    at java.awt.EventQueue$3.run(EventQueue.java:660) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:671) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97) 
+1

親密的線153在您的來源? – gks

+0

謝謝,忘了Application.java:153是this.game.paint(g2); – wonton

+2

1st不要覆蓋頂層容器的'paint'方法。使用類似'JPanel'或'JComponent'的東西,你會獲得更好的性能。在所述組件中第二次覆蓋'paintComponent'。第三總是調用'super.paintXxx'。很多去這些方法,你真的不應該繞過它們沒有很好的理由 – MadProgrammer

回答

0

顯然,在設置可見性/大小後設置窗口的圖標圖像是一個壞主意。

this.app.setSize(this.maxX, this.maxY); 
    this.mmenu.setVisible(true); 
    app.setVisible(true); 
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    app.setAlwaysOnTop(true); 

移動這條線

this.app.setIconImage(new ImageIcon("mushy.jpg").getImage()); 

後拋出的NullPointerException停止。獲得的經驗:首先設置窗口圖標。