當我單擊此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'");
}
}
}
該應用程序將列出患者報告的變量並告訴用戶他們的報告中是否使用了錯誤的輸入。
您能給出完整的跟蹤嗎? – Ilya 2012-03-07 14:23:57
異常情況? – talnicolas 2012-03-07 14:24:13
你可以發佈堆棧跟蹤 – PTBG 2012-03-07 14:24:18