2012-03-07 85 views
-5

當我單擊此Java應用程序中的按鈕時,會收到java.lang.NullPointerException。java.lang.NullPointerException方法

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

public class MRA_JFrame extends JFrame implements ActionListener 
{ 
    private JTextField ageField; 
    private JTextField smokesField; 
    private JTextField overweightField; 
    private JButton reportButton; 
    private JTextArea log; 
    private Patient patient; 

    public MRA_JFrame() 
    { 
     super("GDM's Medical risk assessment"); 
     setSize(500, 300); 
     setLocation(200, 200); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 

     setupGUI(); 

     setVisible(true); 
     ageField.requestFocus(); 
    } 

    public void setupGUI() 
    { 
     JPanel westPanel = setupWestPanel(); 
     JPanel centrePanel = setupCenterPanel(); 

     getContentPane().setLayout(new BorderLayout(5, 5)); 
     getContentPane().setBackground(Color.DARK_GRAY); 
     getContentPane().add(westPanel, BorderLayout.WEST); 
     getContentPane().add(centrePanel, BorderLayout.CENTER); 
    } 

    public JPanel setupCenterPanel() 
    { 
     Font f = new Font("Courier New", Font.PLAIN, 12); 

     log = new JTextArea(5, 20); 
     log.setEditable(false); 
     log.setFont(f); 

     JPanel p = new JPanel(new GridLayout(1, 1)); 
     p.add(new JScrollPane(log)); 
     return p; 
    } 

    public JPanel setupWestPanel() 
    { 
     JPanel wnPanel = setupWestNorthPanel(); 
     JPanel wsPanel = setupWestSouthPanel(); 

     JPanel p = new JPanel(new BorderLayout()); 
     p.add(wnPanel, BorderLayout.NORTH); 
     p.add(wsPanel, BorderLayout.SOUTH); 
     return p; 
    } 

    public JPanel setupWestNorthPanel() 
    { 
     ageField = new JTextField(5); 
     smokesField = new JTextField(5); 
     overweightField = new JTextField(5); 

     JPanel agePanel = new JPanel(new GridLayout(1, 2)); 
     agePanel.add(new JLabel("Age: ", JLabel.RIGHT)); 
     JPanel p = new JPanel(); 
     p.add(ageField); 
     agePanel.add(p); 

     JPanel smokesPanel = new JPanel(new GridLayout(1, 2)); 
     smokesPanel.add(new JLabel("Smokes: ", JLabel.RIGHT)); 
     p = new JPanel(); 
     p.add(smokesField); 
     smokesPanel.add(p); 

     JPanel overweightPanel = new JPanel(new GridLayout(1, 2)); 
     overweightPanel.add(new JLabel("Overweight: ", JLabel.RIGHT)); 
     p = new JPanel(); 
     p.add(overweightField); 
     overweightPanel.add(p); 

     JPanel np = new JPanel(); 
     np.setLayout(new BoxLayout(np, BoxLayout.Y_AXIS)); 
     np.add(agePanel); 
     np.add(smokesPanel); 
     np.add(overweightPanel); 

     return np; 
    } 

    public JPanel setupWestSouthPanel() 
    { 
     reportButton = new JButton("Report"); 
     reportButton.addActionListener(this); 

     JPanel p = new JPanel(); 
     p.add(reportButton); 

     return p; 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) 
    { 
     if(e.getSource() == reportButton) 
     { 
      System.out.println("I'm Clicked!"); 
      patient.setAge(ageField, log); 
     } 
    } 

Patient類如下。

import javax.swing.*; 

public class Patient 
{ 
    private int age; 
    private String smoker; 
    private String overweight; 

    // Consturctor 
    public Patient() 
    { 
     age = 0; 
     smoker = "n"; 
     overweight = "n"; 
    } 

    public void setAge(JTextField age, JTextArea log) 
    { 
     try 
     { 
      this.age = Integer.parseInt(age.getText()); 

     } 
     catch(NumberFormatException nfe) 
     { 
      log.append("Age must be an integer"); 

     } 
    } 

    public void setSmoker(JTextField smoker, JTextArea log) 
    { 
     if(smoker.getText().equalsIgnoreCase("y") || smoker.getText().equalsIgnoreCase("n")) 
     { 
      this.smoker = smoker.getText(); 

     } 
     else 
     { 
      log.append("Smokes must be one 'y', 'Y', 'n' or 'N'"); 
     } 
    } 

    public void setOverweight(JTextField overweight, JTextArea log) 
    { 
     if(overweight.getText().equalsIgnoreCase("y") || overweight.getText().equalsIgnoreCase("n")) 
     { 
      this.overweight = overweight.getText(); 

     } 
     else 
     { 
      log.append("overweight must be one 'y', 'Y', 'n' or 'N'"); 
     } 
    } 
}   

該應用程序將列出患者報告的變量並告訴用戶他們的報告中是否使用了錯誤的輸入。

+2

您能給出完整的跟蹤嗎? – Ilya 2012-03-07 14:23:57

+0

異常情況? – talnicolas 2012-03-07 14:24:13

+0

你可以發佈堆棧跟蹤 – PTBG 2012-03-07 14:24:18

回答

2

問題是在actionPerformed()方法。類變量patientnull

你可以做一個null檢查這樣的...

public void actionPerformed(ActionEvent e) 
{ 
    if(e.getSource() == reportButton && patient != null) 
    { 
     System.out.println("I'm Clicked!"); 
     patient.setAge(ageField, log); 
    } 
} 

或者你可以initalize變量...

public void actionPerformed(ActionEvent e) 
{ 
    if (patient == null) 
    { 
     patient = new Patient(); 
    } 

    if(e.getSource() == reportButton) 
    { 
     System.out.println("I'm Clicked!"); 
     patient.setAge(ageField, log); 
    } 
} 

或者你initalize的變量時,你declare它...

private Patient patient = new Patient(); 
+0

啊,我不知道你可以做空檢查!輝煌!感謝您的幫助和擴展我的知識。 – Leon 2012-03-07 14:33:32

+0

沒問題,我們都在這裏。 :-)不要忘記接受答案。 – alexvetter 2012-03-07 14:44:32

+0

您可以通過單擊向上和向下投票按鈕下方的綠色複選按鈕來完成此操作。 – alexvetter 2012-03-07 14:48:17

3

您尚未初始化您的patient變量,因此您試圖訪問空對象的屬性。 替換此

private Patient patient; 

與此

private Patient patient = new Patient(); 
+0

謝謝!我一直在看它一會兒,所以我只是看到它,新鮮的眼睛顯然幫助:) – Leon 2012-03-07 14:25:55

+0

@ user1254899然後接受答案:) – 2012-03-07 14:44:04

2

你還沒有ini對patient類字段進行了分類,例如:

private Patient patient = new Patient(); 
相關問題