1

我一直在尝试创建一个JFrame计算器,但出现错误java.lang.nullpointer.exception。它说我在我说的那一行有问题:

GuiCalc go = new GuiCalc();

以下是所有代码:

package home.personalprojects.jordan;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class GuiCalc extends JFrame
{

    private JButton calculate;
    private JTextField num1field, num2field;
    private JComboBox operationbox;
    private JLabel label1, label2, label3;
    private String[] operationposs = {"+", "-", "*", "/"};
    String operation;
    int num1, num2, answer;

    public GuiCalc(){

        super("Calculator");
        setLayout(new FlowLayout());

        operationbox = new JComboBox(operationposs);

        calculate = new JButton("Calculate");
        calculate.setToolTipText("Enter Two Numbers And Then Select An Operation To Find An Answer");

        label1 = new JLabel("Number 1: "); 
        num1field = new JTextField("", 10);
        label2 = new JLabel("Number 2: ");
        num2field = new JTextField("", 10);

        operationbox.addItemListener(
            new ItemListener(){
                public void itemStateChanged(ItemEvent event){
                    if(event.getStateChange() == ItemEvent.SELECTED){

                        int temp;
                        temp = operationbox.getSelectedIndex();
                        switch(temp){
                        case 0:
                            answer = num1 + num2;
                        case 1:
                            answer = num1 - num2;
                        case 2:
                            answer = num1 * num2;
                        case 3:
                            answer = num1 / num2;

                    }
                }
            }
        });


        calculate.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    JOptionPane.showMessageDialog(null, String.format("The Answer: " + answer, event.getActionCommand()));
                }
            }
        );

        add(label1);
        add(num1field);
        add(label2);
        add(operationbox);
        add(label3);
        add(num2field);
        add(calculate);

    }

    public static void main(String[] args)
    {

        GuiCalc go = new GuiCalc();
        go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        go.setSize(500,500);
        go.setVisible(true);


    }

}
4

3 回答 3

2

当我运行你的代码时,我得到......

Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1086)
    at java.awt.Container.add(Container.java:998)
    at javax.swing.JFrame.addImpl(JFrame.java:562)
    at java.awt.Container.add(Container.java:410)
    at testcalc.TestCalc.<init>(TestCalc.java:79)
    at testcalc.TestCalc.main(TestCalc.java:87)

这指向

add(label3);

这表明label3尚未初始化。

于 2013-08-20T02:20:27.917 回答
0

正如MadProgrammer所说,label3在您调用add. JFrame继承自java.awt.Component,如果传递一个空参数,它的方法add(Compontent comp)抛出异常。确保在构建JLabel之前add构建它。

于 2013-08-20T02:34:35.223 回答
0

在将它添加到此之前,您需要为 label3 分配一个值:

label3 = new JLabel("");

或者

label3 = null;
if (label3 != null) this.add(label3);
于 2013-08-20T02:49:01.880 回答