CardPanel:获取空指针异常-IntelliJ

时间:2018-12-02 11:52:04

标签: java swing user-interface nullpointerexception awt

我想用Java创建我的第一个人,我正在使用IntelliJ的GridLayout。我还必须使用CardLayout来更改面板并具有不同的屏幕。我已经设置了IntellJ提供的the demo中的所有内容。

但是,我得到一个java.lang.NullPointerException。错误消息如下:

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)

我还设置了其他六个屏幕,这些屏幕返回了根面板,所以我不会在此处复制并粘贴代码。他们只有一个面板和一个get方法来返回面板。就像在演示中一样。

这是我的主窗口代码,称为UserInterface。你们能告诉我,我的错误在哪里以及为什么得到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);
    }
}

致以问候,谢谢!

0 个答案:

没有答案