チェックインフォームを作成しようとしていますが、うまく設計しようとしていますが、このエラー 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)