1

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);
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

2

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.

Community
  • 1
  • 1
Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
  • In other words, the function of `getLayout()` is to get the layout of the panel? Pardon me for using the wrong terminologies – hahaashton Jul 26 '14 at 11:00
  • because the default layout for swing components is the null layout – firephil Jul 26 '14 at 11:11
  • 1
    @CaptainGiraffe Your "non-final variable" link is no longer correct with Java version 1.8 and later, which introduces the concept of "effectively final". The OP may be using 1.8, on which the code will compile correctly. http://stackoverflow.com/questions/20938095/difference-between-final-and-effectively-final – Erwin Bolwidt Jul 26 '14 at 11:17
  • All is working now. Thank you guys. The problem now is the 'next' button yields no result although no error is displayed. Is there anything else wrong with my code? – hahaashton Jul 26 '14 at 12:12
  • @user3879549 add the code for LoginPanel – firephil Jul 27 '14 at 19:46
  • Alright, I've followed up to that already – hahaashton Jul 28 '14 at 08:15
1
//change to 
    final CardLayout cardLayout = new CardLayout();
    final JPanel cards = new JPanel(cardPanel);
    cards.setLayout(cardLayout);

You have to declare the variables final so you can refer to them from an anonymus inner class new ActionListener() {}

Also use the naming convention of java for naming classes => start with capital letter i.e InsuranceMain, LoginPanel

firephil
  • 830
  • 10
  • 18