java - JPanel 和 CardLayout 出现 NullPointerException 错误

标签 java swing null jpanel cardlayout

我正在开发类酒店管理软件,我的代码遇到了一些问题。此时,我只是尝试将我在单独的类中创建的 JPanel 添加到我的主 gui 中。任何帮助将不胜感激。 〜谢谢!

Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at hotelManage.HotelSystem.showGUI(HotelSystem.java:75)
    at hotelManage.HotelSystem.<init>(HotelSystem.java:27)
    at hotelManage.HotelSystem.main(HotelSystem.java:115)

注意:错误发生在“jpanel.add("Room", room.getRoomPanel());”行

代码: HotelSystem.java

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class HotelSystem extends JFrame implements ActionListener {
    /**
     * 
     */
    private static final long serialVersionUID = 1840835913045151061L;

    private JFrame mainFrame;
    private JPanel mainPanel;
    private JButton btnRoom;
    private JButton btnCustomer;
    private JButton btnOrder;
    private JButton btnSearch;
    RoomSystem room;

    //RoomSystem room = new RoomSystem();


    public HotelSystem(){
        prepareGUI();
        showGUI();
        registerListeners();


    }

    private void prepareGUI(){
        mainFrame = new JFrame("Hotel Management System");
        mainFrame.setSize(500,500);
        mainFrame.setLayout(new GridLayout(1,1));

        btnRoom = new JButton("Room Editor");
        btnCustomer = new JButton("Customer Editor");
        btnOrder = new JButton("Order");
        btnSearch = new JButton("Search");

        //main panel
        mainPanel = new JPanel();
        mainPanel.setLayout(new FlowLayout());

        mainFrame.add(mainPanel);
        mainFrame.setVisible(true);




    }

    private void showGUI(){
        final JPanel jpanel = new JPanel();
        jpanel.setBackground(Color.CYAN);
        jpanel.setSize(300,300);

        CardLayout cLayout = new CardLayout();
        cLayout.setHgap(5);
        cLayout.setVgap(5);
        jpanel.setLayout(cLayout);



        JPanel btnPanel = new JPanel(new FlowLayout());
        btnPanel.add(btnRoom);
        btnPanel.add(btnCustomer);
        btnPanel.add(btnOrder);
        btnPanel.add(btnSearch);
        jpanel.add("Button", btnPanel);
        jpanel.add("Room", room.getRoomPanel());

        mainPanel.add(jpanel);
        mainPanel.setVisible(true);  


    }

    public void registerListeners(){
        //register all buttons to self
        btnRoom.addActionListener(this);
        btnCustomer.addActionListener(this);
        btnOrder.addActionListener(this);
        btnSearch.addActionListener(this);

    } // end registerListeners

    public void actionPerformed(ActionEvent e){
        System.out.println(e.getActionCommand());
        //check all button presses and send
        //control to appropriate methods
       if (e.getSource() == btnRoom){

        } else if (e.getSource() == btnCustomer){

        } else if (e.getSource() == btnOrder){

        } else if (e.getSource() == btnSearch){

        } else {
            //lblOutput.setText("something went wrong");
        } // end if


    } // end actionPerformed


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

    }


}

RoomSystem.java

import java.awt.*;

import javax.swing.*;



public class RoomSystem {


    //private JTextField roomName;
    private JButton btnEdit;
    private JPanel roomPanel;
    //private JButton roomCancel;

    //array here

    public RoomSystem(){
        btnEdit = new JButton("Create");

        JPanel roomPanel = new JPanel(new FlowLayout());
        roomPanel.add(btnEdit);


        roomPanel.setVisible(true);

    }



    public JPanel getRoomPanel() {
        return roomPanel;
    }


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




}

最佳答案

jpanel.add("Room", room.getRoomPanel());

您从未初始化过房间

RoomSystem room;

即使您确实初始化了 RoomSystem room = new RoomSystem(),您的 RoomSystem 类中仍然存在另一个问题。您已经隐藏了 roomPanel,因此当您尝试调用 getRoomPanel() 时,类成员为 null。在您的构造函数中,更改

// shadowing the class field roomPanel
JPanel roomPanel = new JPanel(new FlowLayout());  

to 

roomPanel = new JPanel(new FlowLayout());

关于java - JPanel 和 CardLayout 出现 NullPointerException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23177175/

相关文章:

java - 如何从具有不同字段的同一类创建两个 json 文件

java - 排除 Jersey2 响应中的字段

java - 使用 Java BigDecimal 仍然不能正确解决

java - 通过交换 UIDefaults 改变外观

SQL根据一个字段的值在两个字段之间进行选择

java - TimerTask 停止所有其他执行

java - 如何通过单击按钮打开新窗口

java - 如何在不影响原始 JPanel 的情况下更改 JPanel 属性?

asp.net-mvc-3 - 在 Post 上,下拉列表 SelectList.SelectedValue 为 null

mysql - 处理选择查询mysql上的空值