-2

我正在尝试制作一个签到表单,并且我正在尝试将其设计得很好,但我收到了这个错误 NullPointer 异常。这是我的代码:

import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class CheckIn extends JPanel{

    private String[] text = {"  First Name","  Last Name","  Age","  Gender","  Contact No",
            "  Address","  Room Type","  Room No","  Time In","  How many days","  Payment Method"};
    private JPanel designPanel,bottomRightPanel,bottomLeftPanel;
    private String[] genderJCB = {"- Select Gender- ","Male","Female"};
    private String[] roomJCB = {"- Select Room Type -","Classic","Deluxe","Presidential"};
    private JButton submit,cancel;
    private JRadioButton fullRB,depositRB;
    private buttonHandler bh;
    JTextField[] textFields;
    private JComboBox<String> genderBox,roomTypeBox ;
    public CheckIn(){

        //Container settings
        setLayout(null);
        setBackground(new Color(70,70,70));
        //title
        JLabel title = new JLabel("Personal Information",SwingConstants.CENTER);
        title.setBounds(0,0,300,45);
        title.setForeground(Color.ORANGE);
        title.setFont(new Font("Arial",Font.BOLD,13));
        title.setBorder(BorderFactory.createMatteBorder(0,0,1,0,Color.BLACK));  
        add(designPanel());
        add(title);
        //fields
    }
    public JPanel designPanel()
    {
        //Sub container settings
        designPanel = new JPanel( new GridLayout(12,2));
        designPanel.setBounds(0,45,300,505);
        designPanel.setBackground(new Color(80,80,80));
        designPanel.add(bottomLeftPanel());
        designPanel.add(bottomRightPanel());
        return designPanel;
    }
    public JPanel bottomLeftPanel()
    {
        JPanel bottomLeftPanel = new JPanel();
        bottomLeftPanel.setLayout(null);
        bottomLeftPanel.setBorder(BorderFactory.createMatteBorder(1,0,0,0,Color.GRAY));
        bottomLeftPanel.setBackground(new Color(70,70,70));
        bottomButtons();
        return bottomLeftPanel;
    }
    public JPanel bottomRightPanel()
    {
        JPanel bottomRightPanel = new JPanel();
        bottomRightPanel.setLayout(null);
        bottomRightPanel.setBorder(BorderFactory.createMatteBorder(1,0,0,0,Color.GRAY));
        bottomRightPanel.setBackground(new Color(70,70,70));
        bottomButtons();
        return bottomRightPanel;
    }
    public void bottomButtons()
    {
        bh = new buttonHandler();
        cancel = new JButton("Cancel");
        cancel.setBounds(25,4,100,32);
        cancel.addActionListener(bh);
        bottomLeftPanel.add(cancel);
        submit = new JButton("Submit");
        submit.setBounds(25,4,100,32);
        submit.addActionListener(bh);
        bottomRightPanel.add(submit);
    }
    public void components()
    {
        JLabel[] labels = new JLabel[text.length];
        JTextField[] textFields = new JTextField[text.length];
        JPanel[] containerPanel = new JPanel[text.length];



        for(int x = 0; x < text.length; x++){
            //labels
            labels[x] = new JLabel(text[x]);
            labels[x].setForeground(Color.WHITE);
            labels[x].setBorder(new CompoundBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY),
                    BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK)));
            designPanel.add(labels[x]);
            containerPanel[x]= new JPanel();
            containerPanel[x].setLayout(null);
            containerPanel[x].setBackground(new Color(80,80,80));
            containerPanel[x].setBorder(new CompoundBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY),
                    BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK)));
            designPanel.add(containerPanel[x]);
            //text fields and etc
            if(x==3){
                genderBox = new JComboBox(genderJCB);
                genderBox.setBounds(0,10,120,25);
                genderBox.setBackground(Color.WHITE);
                containerPanel[x].add(genderBox);
            }
            else if(x==6){
                roomTypeBox = new JComboBox(roomJCB);
                roomTypeBox.setBounds(0,10,145,25);
                roomTypeBox.setBackground(Color.WHITE);
                containerPanel[x].add(roomTypeBox);
            }
            else if(x==8){
                SpinnerDateModel model = new SpinnerDateModel();
                model.setCalendarField(Calendar.MINUTE);
                JSpinner spinner= new JSpinner();
                spinner.setModel(model);
                spinner.setEditor(new JSpinner.DateEditor(spinner, "h:mm a  "));
                spinner.setBounds(0,10,145,25);
                  containerPanel[x].add(spinner);
            }
            else if (x==10){
                ButtonGroup group = new ButtonGroup();
                fullRB = new JRadioButton("Full");
                fullRB.setBounds(0,10,50,25);
                fullRB.setBackground(new Color(80,80,80));
                fullRB.setForeground(Color.white);
                depositRB = new JRadioButton("Deposit");
                depositRB.setBounds(60,10,70,25);
                depositRB.setBackground(new Color(80,80,80));
                depositRB.setForeground(Color.white);
                group.add(fullRB);
                group.add(depositRB);
                containerPanel[x].add(fullRB);
                containerPanel[x].add(depositRB);
            }
            else {
            textFields[x] = new JTextField();
            textFields[x].setBounds(0,10,145,25);
            containerPanel[x].add(textFields[x]);
            }   
        }
    }


    private class buttonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){

            if(e.getSource() == cancel){

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

            }
        }
    }

}

对不起,如果我的工作如此混乱。我正在尽力简化它。这就是我想出的。我怎样才能简化我的代码?以及如何解决我的错误。请帮助和抱歉一个菜鸟问题。这是堆栈跟踪:

Exception in thread "main" java.lang.NullPointerException
    at CheckIn.bottomButtons(CheckIn.java:73)
    at CheckIn.bottomLeftPanel(CheckIn.java:55)
    at CheckIn.designPanel(CheckIn.java:45)
    at CheckIn.<init>(CheckIn.java:30)
    at HotelMainGUI.leftForms(HotelMainGUI.java:118)
    at HotelMainGUI.leftPanel(HotelMainGUI.java:112)
    at HotelMainGUI.subFrame(HotelMainGUI.java:32)
    at HotelMainGUI.<init>(HotelMainGUI.java:21)
    at HotelMainGUI.main(HotelMainGUI.java:189)
4

1 回答 1

0

NullPointerException来是因为这些代码序列:

     CheckIn.designPanel() -> bottomLeftPanel() ->  bottomButtons()

在里面bottomButons(),你有一行:

    bottomRightPanel.add(submit);

此时,您bottomRightPanel尚未初始化。

改变designPanel方法如下:

   public JPanel designPanel() {
    //Sub container settings
    designPanel = new JPanel( new GridLayout(12,2));
    designPanel.setBounds(0,45,300,505);
    designPanel.setBackground(new Color(80,80,80));
    designPanel.add(bottomRightPanel());
    designPanel.add(bottomLeftPanel());
    return designPanel;
}

designPanel.add(bottomRightPanel());之前搬过designPanel.add(bottomLeftPanel());

希望这能解决您的问题。

此外,还有很大的代码重构空间,例如在你的bottomRightPanelandbottomLeftPanel中,下面的语句似乎很常见或至少相似。在这种情况下,您可以创建一个方法,如下所示:

 public JPanel createPanel(){
    JPanel panel = new JPanel();
    panel.setLayout(null);
    panel.setBorder(BorderFactory.createMatteBorder(1,0,0,0,Color.GRAY));
    panel.setBackground(new Color(70,70,70));
    return pannel;
}

并在您的方法中添加其他步骤,designPanel()例如:

   public JPanel designPanel() {
    //Sub container settings
    designPanel = new JPanel( new GridLayout(12,2));
    designPanel.setBounds(0,45,300,505);
    designPanel.setBackground(new Color(80,80,80));
    JPanel leftPanel = createPanel();
    JPanel rightPanel = createPanel();
    createBottomButtons(leftPanel, rightPanel);
    designPanel.add(leftPanel );
    designPanel.add(rightPanel);
    return designPanel;
}

如果您注意到我删除了bottomButtons();from createPanel(),则添加了designPanel(). 也将leftPanelandrightPanel作为参数传递给此方法。

我希望你可以自己进行剩余的重组。祝你好运!!

于 2012-10-06T15:38:34.867 回答