我正在做一個遊戲,我有幾個不同的課,因爲我不喜歡把所有東西放在一個。我有一個GameView類和一個球員類。問題是我從GameView調用玩家類中的方法。但它給了我一個空指針錯誤。下面是我的代碼:空指針錯誤?
GameVie類:
Panel p2 = new Panel();
p2.setLayout(new BorderLayout());
JLabel player1Lbl = new JLabel("Player 1");
p2.add(player1Lbl, BorderLayout.WEST);
player.enterNameP1(); //Having an error here.
player1Lbl.setText(player.enterNameP1());
玩家等級:
public class Players
{
//storing the player 1 name
private String p1name;
//storing the player 2 name
private String p2name;
/**
* Constructor for objects of class Players
*/
public Players()
{
this.p1name = p1name;
this.p2name = p2name;
}
/**
*Enter the player 1 name in a dialog box
*/
public String enterNameP1() //It was public void before but it wasnt accepting a void method in the gameView so I changed it to String
{
this.p1name = JOptionPane.showInputDialog("Please enter player 1 name","Specify name");
return p1name;
}
-1是的,這些存在。什麼表達導致了它?它幾乎總是*相同的東西*(缺少創建對象)並且「過於本地化」。 – 2012-04-13 20:57:11