2012-10-06 103 views
-2

我試圖做一個簽入表單,我試圖很好地設計它,但我得到這個錯誤NullPointer異常。 這裏是我的代碼:Java NullPointerException和簡化代碼

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){ 

      } 
     } 
    } 

} 

很抱歉,如果我的工作就是這樣一個爛攤子。我正在盡我所能來簡化它。這就是我所想的。我怎樣才能簡化更多我的代碼?以及我如何解決我的錯誤。請幫忙,併爲noob問題感到抱歉。 這裏是堆棧跟蹤:

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) 
+5

沒有引發它的指定行的'NullPointerException'是__completely__沒用。不要粗魯,但你認爲用戶會閱讀整個代碼來想象它會發生什麼? – Jack

+0

你可以添加堆棧跟蹤嗎? – Puckl

+0

@Vincenzo:關於你的評論,''wtf is this => private JComboBox ?'' - 你看到那個構造有什麼錯誤?它對我來說看起來非常好。 –

回答

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

希望這可以解決您的問題,感動designPanel.add(bottomRightPanel());

此外,還有很多代碼改變空間,例如,在你的bottomRightPanelbottomLeftPanel,下面的陳述似乎是常見或至少相似。在這種情況下,你可以創建一個單一的方法如下:

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; 
} 

如果您發現我從createPanel()刪除bottomButtons();補充說,在designPanel()。也通過leftPanelrightPanel作爲該方法的參數。

我希望你可以自己進行重組。祝你好運!!

+0

如果這是正確的信息給你閱讀所有的代碼,我們大多數人都太懶惰:P – Jimmt

+0

我沒有讀完整個代碼。我只是看着異常消息,它說它來自'CheckIn。bottomButtons(CheckIn.java:73)'方法。唯一可疑的是:如果'rightPanel'沒有被初始化。在同一過程中,我注意到,在左右面板創建方面沒有太大差異。只回答了這兩個意見。 –