2

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".

Paul Lo
  • 6,032
  • 6
  • 31
  • 36
CDickson
  • 195
  • 2
  • 5
  • 15
  • It looks like you never instantiated surface. – Tyler Dec 12 '13 at 16:17
  • 2
    Since `JPanel.add(null)` results in a `NullPointerException`, `p.dealCard();` returns probably `null`. You will see the string **"null"** in your `System.out.println(temp);` statement. – bobbel Dec 12 '13 at 16:20
  • 1
    What is `p.dealCard();`? – Paul Samsotha Dec 12 '13 at 16:20
  • 2
    It would appear that "temp" is null. (BTW, you should have debugged this yourself. Even if you don't have an interactive debugger you can insert a println and see what the value of temp is before the call.) – Hot Licks Dec 12 '13 at 16:25
  • @HotLicks he does have a println. Also, if temp was null wouldn't the error be on temp.addMouseListener()? – bcorso Dec 12 '13 at 16:27
  • @bcorso - But no clue as to what got printed. – Hot Licks Dec 12 '13 at 16:28
  • 1
    @bcorso, good point, I didn't realise this either... So... the NPE can't be on that line, can it? Perhaps, assuming that `p.dealCard()` or `prepareCards()` will set the surface variable to null, it will crash on exactly the OPs line... – bobbel Dec 12 '13 at 16:42

2 Answers2

1

A null pointer exception is telling you that one of your variables is null, and you are using it in an inappropriate way

This mostly occurs when you try to use a method of an object (E.g.):

// gives NPE if temp == null, because null does not have any methods
temp.addMouseListener(cardHandle);

It also occurs when adding null to some collections, E.g. Queue (although some collections allow it):

// gives NPE if temp == null (also if surface == null) 
surface.add(temp);

To debug this in the console you can print the values that are suspect before the null exception occurs:

// 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);
Community
  • 1
  • 1
bcorso
  • 45,608
  • 10
  • 63
  • 75
0

I found the problem myself. It was an off-by-one error. I was trying to fill a 4 by 4 grid using only 14 cards, so when it went to find the 15th it got a null. It now runs! Mostly. Anyway, thanks for the help, you pointed me in the right direction.

CDickson
  • 195
  • 2
  • 5
  • 15