我正在做一个游戏,我有几个不同的课,因为我不喜欢把所有东西放在一个。我有一个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