Java 获取空指针,don';我不知道为什么

Java 获取空指针,don';我不知道为什么,java,nullpointerexception,jpanel,awt,Java,Nullpointerexception,Jpanel,Awt,我正在做一个程序,在一个4 x 4的网格中显示8对面朝下的牌,你需要找到获胜的牌对 我已经编写了这些类,当我尝试运行时,我得到了一个NullPointerException。但我不知道为什么 这是错误所在的代码: public Game(String s) { super(s); JPanel cp = (JPanel)getContentPane(); cp.add("North", scoreLabel); surface = new JPanel();

我正在做一个程序,在一个4 x 4的网格中显示8对面朝下的牌,你需要找到获胜的牌对

我已经编写了这些类,当我尝试运行时,我得到了一个NullPointerException。但我不知道为什么

这是错误所在的代码:

public Game(String s)
{
    super(s);
    JPanel cp = (JPanel)getContentPane();
    cp.add("North", scoreLabel);
    surface = new JPanel();
    surface.setLayout(new GridLayout(4, 4));
    cp.add("Center", surface);
    prepareCards();
    for (int x = 0; x < 16; x++)
    {
        Card temp = p.dealCard();
        System.out.println(temp);
        temp.addMouseListener(cardHandle);
        **surface.add(temp);**
    }
}
public static void main(String args[])
{
    *Game game = new Game("TEST GAME PLEASE IGNORE");*
    game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.setSize(600, 400);
    game.setVisible(true);
}
公共游戏(字符串s)
{
超级(s);
JPanel cp=(JPanel)getContentPane();
cp.add(“北”,分数标签);
表面=新的JPanel();
设置布局(新的网格布局(4,4));
cp.add(“中心”,表面);
prepareCards();
对于(int x=0;x<16;x++)
{
卡温度=p.dealCard();
系统输出打印项次(温度);
临时添加鼠标侦听器(卡片手柄);
**表面。添加(温度)**
}
}
公共静态void main(字符串参数[])
{
*游戏=新游戏(“请忽略测试游戏”)*
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
游戏设置大小(600400);
game.setVisible(true);
}
下面的错误(这不是很有帮助)

线程“main”java.lang.NullPointerException中的异常 位于java.awt.Container.addImpl(未知源) 位于java.awt.Container.add(未知源) 在certClasses.Game.(Game.java:39) 在certClasses.Game.main(Game.java:44) 第39行为双星号(****),第44行为单星号(**)


我已经在谷歌上搜索了错误,但没有得到任何有用的信息(stackoverflow任务结束时,大多数情况下不太可能帮助其他人)。当我可以的时候,我会在pastebin上发布整个代码;我现在不在家,pastebin被阻止为“个人网络存储和备份”。

空指针异常告诉您,您的一个变量为空,并且您正在以不适当的方式使用它

这主要发生在您尝试使用对象的方法时(例如):

null
添加到(尽管):

要在控制台中对此进行调试,您可以在出现
null
异常之前打印可疑值

// you actually have this in your code, so you should see 'null' printed
Card temp = p.dealCard();
System.out.println(temp);
// you should also print this out since surface could possibly be the null nulprit
System.out.println(surface);

我自己发现了问题。这是一个一错再错的错误。我试图用14张卡片填充一个4乘4的网格,所以当它找到第15张时,得到了一个空值。它现在运行!大部分。无论如何,感谢您的帮助,您为我指明了正确的方向。

看起来您从未实例化过surface。因为
JPanel.add(null)
导致
NullPointerException
p.dealCard()
返回可能
null
。您将在
System.out.println(temp)中看到字符串“null”语句。什么是
p.dealCard()?似乎“temp”为空。(顺便说一句,你应该自己调试这个。即使你没有交互式调试器,你也可以插入一个println,看看调用前temp的值。)@bcorso,说得好,我也没有意识到这一点。。。所以NPE不可能在那条线上,是吗?也许,假设
p.dealCard()
prepareCards()
将surface变量设置为null,它将恰好在OPs行上崩溃。。。
// gives NPE if temp == null, because null does not have any methods
temp.addMouseListener(cardHandle);
// gives NPE if temp == null (also if surface == null) 
surface.add(temp);
// you actually have this in your code, so you should see 'null' printed
Card temp = p.dealCard();
System.out.println(temp);
// you should also print this out since surface could possibly be the null nulprit
System.out.println(surface);