Domanda

How can I properly add a JPanel that is defined in the "RoomSystem.Java" code?

The ERROR:

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)

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

Full Code:

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;

    }



}
È stato utile?

Soluzione

The Biggest problem you have is improperly/incorrectly/unnecessarily using inheritance. You think that just because RoomSystem extends MainSystem, that the roomPanel will be shared between the two classes. It doesn't work like that. So roomPanel in the MainSystem is never initialized, which is causing the NullPointerException

You need to rethink your class design, and without using inheritance, because it doesn't look like you know how to use it correctly, and because it is not appropriate.

But just yo explain to you what's going on.

You have RoomSystem, which extends MainSystem. So RoomSystem is it's own entity, but has (not share) the same properties and method at MainSystem, but they have absolutely no relation in terms of object references. So when you call setRoomPanel(new JPanel()); in the RoomSystem, you are only setting the roomPanel in the RoomSystem class, not the MainSystem class.

A static field roomPanel, may seem like the correct fix for beginners, but it's totally inappropriate. If you want RoomSystem to be it's own panel, then you should make it extends Panel, and not MainSystem, and you can just add the RoomSystem panel to the MainSystem frame.

If you need to pass something, like the CardLayout from the MainSystem to the RoomSystem, then you can inject it through the constructor.

public class RoomSystem extends JPanel {
    private CardLayout;

    public RoomSystem(CardLayout layout) {
        this.layout = layout;
    }
}

Then you can use the layout from the MainSystem in the RoomSystem class, as they now reference the same object.

Another design option would be to implement an interface with a method that sets the card (in your MainSystem class).

public interface CardViewChanger {
    public void setCard(String card);
    public void previousCard();
}

public class MainSystem extends JFrame implements CardViewChanger {
    RoomSystem roomPanel = new RoomSystem(this);
    CardLayout layout;

    @Override
    public void setCard(String card) {
        layout.show(this, card);
    }

    @Override
    public void previousCard() {
        layout.next();
    }
}

public class RoomSystem extends JPanel {
    CardViewChanger cardChanger;

    public RoomSystem(CardViewChanger cardChanger) {
        this.cardChanger = cardChanger;

         ...
         public void actionPerformed(ActionEvent e) {
             cardChanger.previousCard();
         }
    } 
}

Altri suggerimenti

roomPanel is null. You need to initialize it before adding it to mainPanel.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top