Search code examples
javaswingnullpointerexceptioncardlayout

NullPointerException error and cardlayout


The NullPointerException only comes about when I click the 'next' button. I'm trying to get the frame to transition to another panel using cardlayout, but I have failed to do so.

Second question: How do I get my current panel to transition to the next panel (LoginPanel) in another class? I've already created an instance of it

public class InsuranceMain extends JFrame {

    private CardLayout cardPanel;
    private JPanel buttonPanel;
    private JButton next;
    private JLabel label;

    public InsuranceMain() {

        buttonPanel = new JPanel();
        JPanel card1 = new JPanel();
        JPanel cards = new JPanel(cardPanel);
        LoginPanel loginPanelA = new LoginPanel(this);
        CardLayout cardLayout = new CardLayout();
        cards.setLayout(cardLayout);

        next = new JButton("Next"); // Button
        label = new JLabel("Test");

        // Main frame settings
        setTitle("Travel Insurance Application");
        setSize(300, 300);
        setLayout(new FlowLayout());
        setVisible(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        buttonPanel.add(next);
        add(buttonPanel);

        card1.add(label);
        cards.add(card1, "card1");
        cards.add(loginPanelA, "loginPanelA");

        next.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                if(e.getSource() == next) {

                    cardLayout.show(cards, "loginPanelA");
            }}
        });
    }

    public static void main(String[] args) {

        InsuranceMain test = new InsuranceMain();
    }
}

Here's my LoginPanel code. I apologize if it's very disorganized

public class LoginPanel extends JPanel {

    private JButton loginB, registerB, clearB;
    private JTextField nricTextField, passwordTextField;
    private JLabel nric, password, loginTitle;
    private JPanel centerPanel, southPanel;

    private InsuranceMain main;

    public LoginPanel(InsuranceMain main){

        this.main = main;

        // JLabels
        loginTitle = new JLabel("Insurance Login", JLabel.CENTER);
        nric = new JLabel("NRIC: ");
        password = new JLabel("Password: ");

        // JTextField
        nricTextField = new JTextField("");
        passwordTextField = new JTextField("");

        // JButton
        loginB = new JButton("Login");
        loginB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                if(e.getSource() == loginB) {

                    //cardLayout.show(cards, "card1");

            }}
        });
        registerB = new JButton("Register");
        clearB = new JButton("Clear");


        // North Panel
        setLayout(new BorderLayout()); // main panel's layout
        add(loginTitle, BorderLayout.NORTH);


        // Center Panel
        centerPanel = new JPanel();
        centerPanel.setLayout(new GridLayout(2,2));
        centerPanel.add(nric);
        centerPanel.add(nricTextField);
        centerPanel.add(password);
        centerPanel.add(passwordTextField);
        add(centerPanel, BorderLayout.CENTER); 

        // South Panel
        southPanel = new JPanel();
        southPanel.setLayout(new FlowLayout());
        southPanel.add(loginB);
        southPanel.add(registerB);
        southPanel.add(clearB);
        add(southPanel, BorderLayout.SOUTH);
    }
}

Solution

  • A NPE in the actionPerformed method can only be caused by cardLayout being null. So instead of

    CardLayout cardLayout = (CardLayout) cards.getLayout(); // yields null
    

    You should create the layout you need.

    CardLayout cardLayout = new CardLayout();
    

    See also Cannot refer to a non-final variable inside an inner class defined in a different method

    See also Erwin's comment.