I'm working on a program that displays 8 pairs of cards face down in a 4 x 4 grid, and you need to find the pairs to win.
I've written the classes, and when I try to run, I'm getting a NullPointerException. But I don't know why.
This is the code where the error sits:
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);
}
Error below (it isn't very helpful).
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at certClasses.Game.<init>(Game.java:39)
at certClasses.Game.main(Game.java:44)
Line 39 is in double asterisks (** **), line 44 is in single asterisks (* *).
I've googled the errors, and didn't get anything helpful (stackoverflow quests closed as being unlikely to help others, mostly). I'll post the whole code on pastebin when I can; I'm not at home right now and pastebin is blocked as "Personal Network Storage and Backup".