-2

I'm writing a program for my school assignment using CardLayout() but I keep getting a null pointer error but I can't figure out why. Can someone please point out the error in my code. The code is incomplete but applet opens and displays buttons but not the card.

The error I get is when I press the Input button (so it's in the ActionListener):

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException at BookCentre.actionPerformed(BookCentre.java:162)

//removed code since assignment is due in a week.
tee
  • 3
  • 2
  • what's on line 162 in your code? I'd imagine it's that you never initialize CardDeck1. I don't see that anywhere in the class – mike Mar 08 '17 at 22:00

1 Answers1

0

The problem is -as @mike suggest- that CardDeck1 variable is never initialized. I initialize it in the declaration only for testing purposes, you must do it in the neccesary part of the code, where you really need it.

    //Only for testing purposes
    // Create JPanel deck and add cards to it
    deck = new JPanel();
    CardDeck1 = new CardLayout();
    deck.setLayout(CardDeck1);
    deck.add(inputCard(), INPUT);

Result:

enter image description here

sirandy
  • 1,834
  • 5
  • 27
  • 32