2012-10-08 139 views
0

錯誤:「線程中的異常」main「java.lang.NullPointerException at com.vipgamming.Frytree.Game.main(Game.java:47) 「Java錯誤「線程異常」main「java.lang.NullPointerException」

我不是一個很好的程序員。只是說和壞Inglish。

Game.java:

package com.vipgamming.Frytree; 

import java.awt.Canvas; 
import java.awt.Dimension; 

import javax.swing.JFrame; 

public class Game extends Canvas implements Runnable { 

private static final long serialVersionUID = 1L; 
public static int width = 300; 
public static int height = width /16 * 9; 
public static int scale = 3; 

private Thread thread; 
private JFrame frame; 
private boolean running = false; 

public Game() { 
    Dimension size = new Dimension(width * scale, height * scale); 
    setPreferredSize(size); 
} 

public synchronized void start() { 
    running = true; 
    thread = new Thread(this, "Display"); 
    thread.start(); 
} 

public synchronized void stop() { 
    running = false; 
    try { 
     thread.join(); 
    }catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
} 

public void run() { 
    while (running) { 
      System.out.println("FryTree...Loading..."); 
     } 
    } 

    public static void main(String [] args) { 
     Game game = new Game(); 
     game.frame.setResizable(true); 
     game.frame.setTitle("Frytree"); 
     game.frame.add(game); 
     game.frame.pack(); 
     game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     game.frame.setLocationRelativeTo(null); 
     game.frame.setVisible(true); 

     game.start(); 
    } 
} 

對不起,我不明白如何發佈代碼(不Inglish.Portuguese)

+1

你爲什麼不創建框架? – BalusC

+1

第47行在哪裏? – jn1kk

回答

4

您使用前需要先實例化框架。

game.frame = new JFrame(); 
game.frame.setResizable(true); 
... 

你也可以把它放在構造函數中:

public Game() { 
this.frame = new JFrame(); 
Dimension size = new Dimension(width * scale, height * scale); 
setPreferredSize(size); 
} 

如果main中定義的設置每次都是相同的,那麼您可以將整個部分分解到構造函數中,而不是僅創建一個空白部分。如果沒有,你總是可以重載的構造是這樣的:

public Game() {//base constructor 
this.frame = new JFrame(); 
Dimension size = new Dimension(width * scale, height * scale); 
setPreferredSize(size); 
} 

public Game(JFrame jframe)//injected frame constructor 
{ 
this.frame = jframe; 
Dimension size = new Dimension(width * scale, height * scale); 
setPreferredSize(size); 
} 

或有遊戲創造

public Game() 
{ 
this.ConstructFrame(); 
Dimension size = new Dimension(width * scale, height * scale); 
setPreferredSize(size); 
} 

private void ConstructFrame() 
{ 
    this.frame = new JFrame(); 
    this.frame.setResizable(true); 
    this.frame.setTitle("Frytree"); 
    this.frame.add(game); 
    this.frame.pack(); 
    this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.frame.setLocationRelativeTo(null); 
    this.frame.setVisible(true); 
} 
+0

感謝您的幫助。 – Malta108

+0

@ user1729846 - :D參見其他一些選項的編輯 –

+1

是的,但我只是在學習,而我陷入了這個錯誤,我會看到如果我這樣做。 – Malta108

2

Game.frame將不會被初始化設置任何地方因此NPE。更好地保持這個框架初始化在一個單獨的init方法:

private void init() { 
    frame = new JFrame(); 
    frame.setSize(400, 300); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setResizable(true); 
    frame.setTitle("Frytree"); 
    frame.add(this); 
    frame.pack(); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 
} 

(也比較好,延長JPanel,而不是重量級AWT Canvas

相關問題