I am relatively new to java and am having trouble using a cardlayout. When I press the button to switch cards I get a nullpointerexception but I do not understand where I could possibly have anything set to null. Any help would be greatly appreciated.

This is my error...

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at ShirtPanel$ShirtListener.actionPerformed(ShirtPanel.java:180)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:20
18)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav
a:2341)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:402)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259
)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:252)
        at java.awt.Component.processMouseEvent(Component.java:6505)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
        at java.awt.Component.processEvent(Component.java:6270)
        at java.awt.Container.processEvent(Container.java:2229)
        at java.awt.Component.dispatchEventImpl(Component.java:4861)
        at java.awt.Container.dispatchEventImpl(Container.java:2287)
        at java.awt.Component.dispatchEvent(Component.java:4687)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832
)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)

        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
        at java.awt.Container.dispatchEventImpl(Container.java:2273)
        at java.awt.Window.dispatchEventImpl(Window.java:2719)
        at java.awt.Component.dispatchEvent(Component.java:4687)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)
        at java.awt.EventQueue.access$000(EventQueue.java:102)
        at java.awt.EventQueue$3.run(EventQueue.java:662)
        at java.awt.EventQueue$3.run(EventQueue.java:660)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:87)
        at java.awt.EventQueue$4.run(EventQueue.java:676)
        at java.awt.EventQueue$4.run(EventQueue.java:674)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:244)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:163)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:151)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)

        at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

This is my code...

import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;

public class Main extends JPanel
{
    public static void main(String[] args) throws IOException
    {
        Main shirts = new Main();
        shirts.runProgram();
    }
    protected JPanel contentPane;
    protected ShirtPanel card1;
    protected ColorPanel card2;

    protected static final String CARD1 = "Card 1";
    protected static final String CARD2 = "Card 2";

    public void runProgram()
    {
        JFrame frame = new JFrame("");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new CardLayout());
        //LogoPanel CLogo = new LogoPanel();
        //CardPanel card = new CardPanel();
        card1 = new ShirtPanel();
        card2 = new ColorPanel();
        contentPane.add(card1, CARD1);
        contentPane.add(card2, CARD2);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setVisible(true);
    }
}

This is my panel where the error occurs

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.border.*;

public class ShirtPanel extends JPanel
{
    Main shirts;
    private JLabel label1, label2, label3, label4, label5, label6;
    private JButton jb1, jb2, jb3, jb4, jb5, jb6;
    private ActionListener shirtL;

    ImageIcon tees = new ImageIcon("tees.gif");
    ImageIcon longSleeve = new ImageIcon("longSleeve.gif");
    ImageIcon polos = new ImageIcon("polos.gif");
    ImageIcon vNeck = new ImageIcon("vNeck.gif");
    ImageIcon tanks = new ImageIcon("tanks.gif");
    ImageIcon kids = new ImageIcon("kids.gif");

    LineBorder red = new LineBorder(Color.red);
    Border raised = BorderFactory.createRaisedBevelBorder();

    public ShirtPanel()
    {
        LogoPanel lp = new LogoPanel();
        setLayout(new BorderLayout());

        add(lp, BorderLayout.NORTH);

        JPanel Box = new JPanel();
        Box.setLayout(new BoxLayout(Box, BoxLayout.X_AXIS));
        Border compoundBorder = BorderFactory.createCompoundBorder(red, raised);
        add(Box, BorderLayout.CENTER);

        ShirtListener shirtL = new ShirtListener();

        //  menu Panel
        JPanel menu = new JPanel();
        menu.setLayout(new BoxLayout(menu, BoxLayout.Y_AXIS));
        menu.setBackground(Color.white);
        menu.setBorder(raised);

        menu.setAlignmentX(Component.LEFT_ALIGNMENT);
        menu.setPreferredSize(new Dimension(250, 580));
        menu.setPreferredSize(new Dimension(250, 580));
        menu.setMaximumSize(new Dimension(250, 580));

        //  icons Panel
        JPanel icons = new JPanel();
        icons.setLayout(new GridLayout(3,2));
        icons.setBackground(Color.white);

        //Short Sleeve
        JPanel g1 = new JPanel();
        g1.setLayout(new BoxLayout(g1, BoxLayout.Y_AXIS));
        g1.setBackground(Color.white);

        JLabel jl1 = new JLabel("<html><font color='red'>Short Sleeve</font></html>\n");
        jl1.setFont(new Font("Serif", Font.TRUETYPE_FONT, 25));
        jl1.setBackground(Color.white);
        g1.add(jl1);

        JButton jb1 = new JButton(tees);
        jb1.setBackground(Color.white);
        jb1.setBorder(BorderFactory.createEmptyBorder());
        jb1.addActionListener(shirtL);
        g1.add(jb1);

        //Long Sleeve
        JPanel g2 = new JPanel();
        g2.setLayout(new BoxLayout(g2, BoxLayout.Y_AXIS));
        g2.setBackground(Color.white);

        JLabel jl2 = new JLabel("<html><font color='red'>Long Sleeve</font></html>\n");
        jl2.setFont(new Font("Serif", Font.TRUETYPE_FONT, 25));
        jl2.setBackground(Color.white);
        g2.add(jl2);

        JButton jb2 = new JButton(longSleeve);
        jb2.setBackground(Color.white);
        jb2.setBorder(BorderFactory.createEmptyBorder());
        jb2.addActionListener(shirtL);
        g2.add(jb2);

        //Polo Shirt
        JPanel g3 = new JPanel();
        g3.setLayout(new BoxLayout(g3, BoxLayout.Y_AXIS));
        g3.setBackground(Color.white);

        JLabel jl3 = new JLabel("<html><font color='red'>Polo Shirts</font></html>\n");
        jl3.setFont(new Font("Serif", Font.TRUETYPE_FONT, 25));
        jl3.setBackground(Color.white);
        g3.add(jl3);

        JButton jb3 = new JButton(polos);
        jb3.setBackground(Color.white);
        jb3.setBorder(BorderFactory.createEmptyBorder());
        jb3.addActionListener(shirtL);
        g3.add(jb3);

        //V-Neck Shirt
        JPanel g4 = new JPanel();
        g4.setLayout(new BoxLayout(g4, BoxLayout.Y_AXIS));
        g4.setBackground(Color.white);

        JLabel jl4 = new JLabel("<html><font color='red'>V-Neck Shirts</font></html>\n");
        jl4.setFont(new Font("Serif", Font.TRUETYPE_FONT, 25));
        jl4.setBackground(Color.white);
        g4.add(jl4);

        JButton jb4 = new JButton(vNeck);
        jb4.setBackground(Color.white);
        jb4.setBorder(BorderFactory.createEmptyBorder());
        jb4.addActionListener(shirtL);
        g4.add(jb4);

        //Tank Tops
        JPanel g5 = new JPanel();
        g5.setLayout(new BoxLayout(g5, BoxLayout.Y_AXIS));
        g5.setBackground(Color.white);

        JLabel jl5 = new JLabel("<html><font color='red'>Tank Tops</font></html>\n");
        jl5.setFont(new Font("Serif", Font.TRUETYPE_FONT, 25));
        jl5.setBackground(Color.white);
        g5.add(jl5);

        JButton jb5 = new JButton(tanks);
        jb5.setBackground(Color.white);
        jb5.setBorder(BorderFactory.createEmptyBorder());
        jb5.addActionListener(shirtL);
        g5.add(jb5);

        //Youth
        JPanel g6 = new JPanel();
        g6.setLayout(new BoxLayout(g6, BoxLayout.Y_AXIS));
        g6.setBackground(Color.white);

        JLabel jl6 = new JLabel("<html><font color='red'>Youth Shirts</font></html>\n");
        jl6.setFont(new Font("Serif", Font.TRUETYPE_FONT, 25));
        jl6.setBackground(Color.white);
        g6.add(jl6);

        JButton jb6 = new JButton(kids);
        jb6.setBackground(Color.white);
        jb6.setBorder(BorderFactory.createEmptyBorder());
        jb6.addActionListener(shirtL);
        g6.add(jb6);

        icons.add(g1);
        icons.add(g2);
        icons.add(g3);
        icons.add(g4);
        icons.add(g5);
        icons.add(g6);
        Box.add(menu);
        Box.add(icons);

    }
    private class ShirtListener extends Main implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            CardLayout cl = (CardLayout)contentPane.getLayout();
            cl.next(contentPane);
            //cl.show(contentPane, (String)event.getItem());
        }
    }
}

I'm impressed. you have a NullPointerException thrown on a line of code that doesn't seem to exist ....

line 180 in the ShirtPanel file, inside the ShirtListener class, but your file only seems to have 171 lines...
in that actionPerformed method, have you checked whether contentPane.getLayout(); returns null?

It's a classic double-declaration problem. YOu have created two different "contentPane" variables.

13  protected JPanel contentPane;

this is the contentPane variable that you use in the actionPeformed and which is initially null, and which is never initialised by you

26  JPanel contentPane = new JPanel();

this is the local variable in runProgram which you initialise, and which "masks" the first contentPane variable, and goes out of scope when runProgram returns.

Edited 2 Years Ago by JamesCherrill

Thank You for the help. I seem to have a bigger problem I have discovered that led to this problem. How come when I extend my Main class in the listener from ShirtPanel, it is not accessing where I set the layout to CardPanel and added card1 and card2 in my runProgram() method.

This article has been dead for over six months. Start a new discussion instead.