方法的java.lang.NullPointerException

时间:2012-03-07 14:22:13

标签: java swing nullpointerexception jbutton

单击此java应用程序中的按钮时收到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);
        }
    }

患者类如下。

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'");
        }
    }
}        

应用程序将列出所报告患者的变量,并告诉用户他们的报告中是否使用了错误的输入。

3 个答案:

答案 0 :(得分:3)

您尚未初始化patient变量,因此您尝试访问null对象的属性。 替换此

private Patient patient;

用这个

private Patient patient = new Patient();

答案 1 :(得分: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();

答案 2 :(得分:2)

您尚未初始化patient类字段,例如:

private Patient patient = new Patient();