Java NullPointerException和简化代码
我试着做一个签入表单,我试着设计好它,但我得到了这个错误NullPointer异常。 这是我的密码:Java NullPointerException和简化代码,java,stack-trace,Java,Stack Trace,我试着做一个签入表单,我试着设计好它,但我得到了这个错误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","
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){
}
}
}
}
import javax.swing.*;
导入javax.swing.border.*;
导入java.awt.event.*;
导入java.awt.*;
导入java.util.*;
公共类签入扩展了JPanel{
私有字符串[]文本={“名字”、“姓氏”、“年龄”、“性别”、“联系人编号”,
“地址”、“房间类型”、“房间号”、“入住时间”、“天数”、“付款方式”};
专用JPanel设计面板、底部右侧面板、底部左侧面板;
私有字符串[]genderJCB={“-选择性别-”,“男性”,“女性”};
私有字符串[]roomJCB={”-选择房间类型-“,”经典“,”豪华“,”总统“};
私有JButton提交、取消;
私人JRadioButton fullRB,存款rb;
私人按钮手柄;
JTextField[]textFields;
私人JComboBox性别箱,roomTypeBox;
公共签入(){
//容器设置
setLayout(空);
挫折背景(新颜色(70,70,70));
//头衔
JLabel title=新的JLabel(“个人信息”,SwingConstants.CENTER);
标题.立根(0,0300,45);
标题.设置前景(颜色.橙色);
title.setFont(新字体(“Arial”,字体粗体,13));
title.setboorder(BorderFactory.createMatteBorder(0,0,1,0,Color.BLACK));
添加(设计面板());
增加(标题);
//田地
}
公共JPanel设计小组()
{
//子容器设置
设计面板=新JPanel(新网格布局(12,2));
设计小组.立根(0,45300505);
设计面板.立根背景(新颜色(80,80,80));
designPanel.add(bottomLeftPanel());
designPanel.add(bottomRightPanel());
返回设计小组;
}
公共JPanel bottomLeftPanel()
{
JPanel bottomLeftPanel=新的JPanel();
bottomLeftPanel.setLayout(null);
bottomLeftPanel.setOrder(BorderFactory.createMatteBorder(1,0,0,Color.GRAY));
底部左侧面板。立根背景(新颜色(70,70,70));
底部按钮();
返回面板;
}
公共JPanel bottomRightPanel()
{
JPanel bottomRightPanel=新的JPanel();
bottomRightPanel.setLayout(空);
bottomRightPanel.setBorder(BorderFactory.CreateMateBorder(1,0,0,Color.GRAY));
右下面板.立根背景(新颜色(70,70,70));
底部按钮();
返回右面板;
}
公用按钮()
{
bh=新按钮手柄();
取消=新的JButton(“取消”);
取消.退步(25,4100,32);;
取消。添加ActionListener(bh);
底部左侧面板。添加(取消);
提交=新按钮(“提交”);
提交.挫折(25,4100,32);;
submit.addActionListener(bh);
底部右侧面板。添加(提交);
}
公共void组件()
{
JLabel[]labels=新的JLabel[text.length];
JTextField[]textFields=新的JTextField[text.length];
JPanel[]containerPanel=新的JPanel[text.length];
对于(int x=0;x
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)
CheckIn.designPanel() -> bottomLeftPanel() -> bottomButtons()
bottomRightPanel.add(submit);
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;
}
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;
}
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;
}