NullPointerException JPanel和CardLayout出错 [英] NullPointerException Error with JPanel and CardLayout

查看:158
本文介绍了NullPointerException JPanel和CardLayout出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为课堂上的酒店管理软件工作,我的代码遇到了一些问题。在这一点上,我只是试图将我在一个单独的类中创建的JPanel添加到我的主gui中。任何帮助将不胜感激。 〜谢谢!

I am working on hotel management software for class and I am running into a few issues with my code. At this point, I am simply trying to add a JPanel I created in a separate class into my main gui. Any Help would be greatly appreciated. ~ Thanks!

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());

Note: The error occurs at the line "jpanel.add("Room", room.getRoomPanel());"

代码:
HotelSystem.java

The code: 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

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。在你的构造函数中,更改

Even if you do initialize it RoomSystem room = new RoomSystem(), you still have another problem in your RoomSystem class. You have shadowed roomPanel, and therefore the class member is null, when you try and call getRoomPanel(). In your constructor, change

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

to 

roomPanel = new JPanel(new FlowLayout());

这篇关于NullPointerException JPanel和CardLayout出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆