JPanel问题:java.lang.NullPointerException

时间:2014-04-26 18:43:01

标签: java swing jframe jpanel panels

如何正确添加“RoomSystem.Java”代码中定义的JPanel?

错误:

Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at hotelManagement.MainSystem.<init>(MainSystem.java:68)
    at hotelManagement.MainSystem.main(MainSystem.java:129)

第68行:getMainPanel().add(roomPanel, "Rooms");

完整代码:

MainSystem.Java:

package hotelManagement;


import java.awt.CardLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MainSystem extends JFrame{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private JFrame mainFrame;
    private JPanel mainPanel;
    private static JPanel roomPanel;
    private JPanel btnPanel;
    private JButton btnRoom;
    private JButton btnCustomer;
    private JButton btnOrder;
    private JButton btnSearch;
    private CardLayout cLayout;
    private JLabel lblUpdate;



    public MainSystem(){
        mainFrame = new JFrame("Hotel Management System");
        mainFrame.setSize(500,300);
        mainFrame.setLayout(new GridLayout(2,0));

        btnRoom = new JButton("Room Editor");
        btnRoom.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                getCLayout().show(getMainPanel(), "Orders");
                System.out.println("You clicked Rooms");
            }
        });      
        btnCustomer = new JButton("Customer Editor");
        btnOrder = new JButton("Order");
        btnSearch = new JButton("Search");

        lblUpdate = new JLabel("Instructions/details will go here.");

        btnPanel = new JPanel();
        btnPanel.add(btnRoom);

        btnPanel.add(btnCustomer);
        btnPanel.add(btnOrder);
        btnPanel.add(btnSearch);
        btnPanel.add(lblUpdate);

        setMainPanel(new JPanel());
        setCLayout(new CardLayout());
        getMainPanel().setLayout(getCLayout());


        getMainPanel().add(btnPanel, "Buttons");
        getMainPanel().add(roomPanel, "Rooms");

        mainFrame.add(getMainPanel());
        mainFrame.setVisible(true);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);




    }


    public JFrame getMainFrame(){
        return mainFrame;
    }

    public void setMainFrame(JFrame mainFrame){
        this.mainFrame = mainFrame;
    }

    public CardLayout getCLayout(){
        return cLayout;
    }

    public void setCLayout(CardLayout cLayout){
        this.cLayout = cLayout; 
    }

    public JPanel getMainPanel(){
        return mainPanel;
    }

    public void setMainPanel(JPanel mainPanel){
        this.mainPanel = mainPanel;
    }

    public JPanel getBtnPanel(){
        return btnPanel;    
    }

    public void setBtnRoom(JPanel btnPanel){
        this.btnPanel = btnPanel;
    }

    public JPanel getRoomPanel() {
        return roomPanel;
    }

    public static void setRoomPanel(JPanel roomPanel) {
        MainSystem.roomPanel = roomPanel;
    }

    public static void main(String[] args) {
        new MainSystem();
    }

}

RoomSystem.Java

package hotelManagement;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JPanel;

public class RoomSystem extends MainSystem {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private JButton btnEdit;
    private JButton btnBack;
    private JComboBox<String> roomType;
    String[] roomArray = { "Penthouse", "Large Room", "Small Room" };

    public RoomSystem() {
        setRoomType(new JComboBox<>(roomArray));

        btnEdit = new JButton("Create");
        btnEdit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                System.out.println("You clicked Create");
            }
        });      

        btnBack = new JButton("Return");
        btnBack.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                System.out.println("You clicked Back");
            }
        });

        Label  lblRoom= new Label("Room Type: ");   


        setRoomPanel(new JPanel());
        getRoomPanel().setLayout(new GridBagLayout());
        GridBagConstraints gridConst = new GridBagConstraints();

        gridConst.gridx = 0;
        gridConst.gridy = 0;
        getRoomPanel().add(lblRoom, gridConst);
        gridConst.gridx = 1;
        gridConst.gridy = 0;
        getRoomPanel().add(getRoomType(), gridConst);

        gridConst.gridx = 0;
        gridConst.gridy = 2;
        getRoomPanel().add(btnEdit, gridConst);
        gridConst.gridx = 1;
        gridConst.gridy = 2;
        getRoomPanel().add(btnBack, gridConst);

    }



    public JComboBox<String> getRoomType(){
        return roomType;

    }

    public void setRoomType(JComboBox<String> roomType){
        this.roomType = roomType;

    }



}

2 个答案:

答案 0 :(得分:2)

您遇到的最大问题是使用继承不正确/错误/不必要。您认为仅仅因为RoomSystem扩展MainSystemroomPanel将在两个类之间共享。它不像那样工作。因此,roomPanel中的MainSystem永远不会被初始化,这会导致NullPointerException

您需要重新考虑您的类设计,并且不使用继承,因为它看起来不像您知道如何正确使用它,并且因为它不合适。

但是,你只是向你解释发生了什么。

您有RoomSystem,其扩展为MainSystem。所以RoomSystem自己的实体,但是 (不是 share MainSystem处的相同属性和方法,但它们在对象引用方面完全没有关系。因此,当您在setRoomPanel(new JPanel());中致电RoomSystem时,您只需在roomPanel课程中设置RoomSystem,而不是MainSystem课程。

static字段roomPanel,对于初学者来说似乎是正确的解决方案,但这完全不合适。如果您希望RoomSystem成为自己的面板,那么您应该将其设为extends Panel,而不是 MainSystem,您只需添加RoomSystem }面板到MainSystem框架。

如果您需要传递某些内容,例如从CardLayoutMainSystem的{​​{1}},那么您可以通过构造函数注入它。

RoomSystem

然后,您可以使用public class RoomSystem extends JPanel { private CardLayout; public RoomSystem(CardLayout layout) { this.layout = layout; } } 类中MainSystem的布局,因为它们现在引用相同的对象。

另一个设计选项是使用设置卡的方法(在RoomSystem类中)实现接口。

MainSystem

答案 1 :(得分:0)

roomPanel为null。您需要先将其初始化,然后再将其添加到mainPanel。