0

I want to create my first guy in Java and I am using the GridLayout by IntelliJ. I also have to use the CardLayout to change the panels and have different screens. I have set up everything like it is in the demo provided by IntellJ.

However, I get a java.lang.NullPointerException. The error message is following:

Exception in thread "main" java.lang.NullPointerException
    at com.intellij.uiDesigner.core.GridLayoutManager.addLayoutComponent(GridLayoutManager.java:134)
    at java.awt.Container.addImpl(Container.java:1130)
    at java.awt.Container.add(Container.java:419)
    at UserInferface.<init>(UserInferface.java:29)

I have six other screens set up which return the root panel, so I am not gonna copy and paste the code here. They just have a panel and a get method to return the panel. Like in the demo.

Here is my main window code called UserInterface. Can you guys tell me, where my error is and why I get a java.lang.NullPointerException?

import screens.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UserInferface extends JDialog {
    private JPanel rootPane;
    private JPanel navigationPane;
    private JButton sellInterfaceButton;
    private JButton restockInterfaceButton;
    private JButton changeCapacityInterfaceButton;
    private JButton changeStockInterfaceButton;
    private JButton orderListInterfaceButton;
    private JButton showStockInterfaceButton;
    private JPanel cardPanel;

    final static String CHANGE_CAPACITY_INTERFACE = "Change capacity";
    final static String CHANGE_STOCK_INTERFACE = "Change stock";
    final static String ORDER_LIST_INTERFACE = "Order list";
    final static String RESTOCK_INTERFACE = "Restock";
    final static String SELL_INTERFACE = "Sell";
    final static String SHOW_STOCK_INTERFACE = "Show stock";

    public UserInferface(){
        setContentPane(rootPane);
        setModal(true);

        cardPanel.add(new ChangeCapacityInterface().getRootPanel());
        cardPanel.add(new ChangeStockInterface().getRootPanel());
        cardPanel.add(new OrderListInterface().getRootPanel());
        cardPanel.add(new RestockInterface().getRootPanel());
        cardPanel.add(new SellInterface().getRootPanel());
        cardPanel.add(new ShowStockInterface().getRootPanel());

        final CardLayout cl =  (CardLayout) cardPanel.getLayout();
        cl.show(cardPanel, SELL_INTERFACE);

        sellInterfaceButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cl.show(cardPanel, SELL_INTERFACE);
            }
        });

        restockInterfaceButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cl.show(cardPanel, RESTOCK_INTERFACE);
            }
        });

        changeCapacityInterfaceButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cl.show(cardPanel, CHANGE_CAPACITY_INTERFACE);
            }
        });

        changeStockInterfaceButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cl.show(cardPanel, CHANGE_STOCK_INTERFACE);
            }
        });

        orderListInterfaceButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cl.show(cardPanel, ORDER_LIST_INTERFACE);
            }
        });

        showStockInterfaceButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cl.show(cardPanel, SHOW_STOCK_INTERFACE);
            }
        });
    }

    public static void main(String[] args) {
        UserInferface userInferface = new UserInferface();
        userInferface.pack();
        userInferface.setVisible(true);
        System.exit(0);
    }
}

Kind regards and Thank You!

Jan
  • 1,180
  • 3
  • 23
  • 60
  • `new ChangeCapacityInterface().getRootPanel()` seems to return `null`. – Seelenvirtuose Dec 02 '18 at 11:57
  • There are many problems with the code. Read the section from the Swing tutorial on [How to use CardLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) for working examples. Some problems I noticed: 1) Why do you have `setContentPane(rootPane)`? The rootPane variable is null. 2) I don't see where you set the layout manager of your `cardPandl`. The layout manager should be set BEFORE adding components to the panel. 3) I don't see where you add the `cardPanel` to the dialog. 4) you don't specify the "constraint" when adding panels to the `cardPanel`. – camickr Dec 02 '18 at 20:03

0 Answers0