首页 > 解决方案 > 导致异常的简单计算器 (GUI)

问题描述

我正在尝试制作一个带有图形用户界面的简单计算器;但是,我无法从文本字段中添加两个值。每次我尝试计算这些值时,我都会收到一个异常。有人可以帮我解决这个问题吗?我对Java相当陌生,我愿意接受反馈和建议。例外在下面。

谢谢,

凯尔

public class Calculator extends Application 
{
private Label firstValue;
private Label secondValue;
private Label sum;
private Button myButton;
private TextField textSum;
private TextField textFirst;
private TextField textSecond;

public String calculation()
{
    String getTextFirst = textFirst.getText();
    String getTextSecond = textSecond.getText();

    int total = Integer.parseInt(getTextFirst) + 
    Integer.parseInt(getTextSecond);
    String realTotal = String.valueOf(total);
    return realTotal;
}

public void start(Stage myStage)
{

    myStage.setTitle("Simple Calculator");
    GridPane rootNode = new GridPane();
    rootNode.setAlignment(Pos.CENTER);
    rootNode.setPadding(new Insets(30));
    Scene myScene = new Scene (rootNode, 400 , 300);
    firstValue = new Label ("First Value: ");
    secondValue = new Label ("Second Value: ");
    sum = new Label ("Sum is: ");

    textFirst = new TextField();
    textSecond = new TextField();
    textSum = new TextField();

    textSum.setEditable(false);
    myButton = new Button ("Calculate");

    rootNode.add(firstValue, 1, 0);
    rootNode.add(textFirst, 2, 0);
    rootNode.add(secondValue, 1, 1);
    rootNode.add(textSecond, 2, 1);
    rootNode.add(sum, 1, 2);
    rootNode.add(textSum, 2, 2);
    rootNode.add(myButton, 2, 3);

    myButton.setOnAction(new ButtonHandler());


    myStage.setScene(myScene);
    myStage.show();



}

class ButtonHandler implements EventHandler<ActionEvent>
{
    public void handle(ActionEvent e)
    {
        Calculator finalTotal = new Calculator();

        myButton.setText("Calculating");

        textSum.setText(finalTotal.calculation());
    }
}


public static void main(String[] args)
{
    launch(args);
}
}

简单的计算器图片

Exception in thread "JavaFX Application Thread" 
java.lang.NullPointerException
at Calculator.calculation(Calculator.java:22)
at Calculator$ButtonHandler.handle(Calculator.java:75)
at Calculator$ButtonHandler.handle(Calculator.java:1)

标签: javajavafx

解决方案


首先,您ButtonHandler正在创建一个与当前显示在屏幕上Calculator的实例无关的新实例Calculator

class ButtonHandler implements EventHandler<ActionEvent>
{
    public void handle(ActionEvent e)
    {
        Calculator finalTotal = new Calculator();

        myButton.setText("Calculating");

        textSum.setText(finalTotal.calculation());
    }
}

这意味着当calculation被调用时......

public String calculation()
{
    String getTextFirst = textFirst.getText();
    String getTextSecond = textSecond.getText();

    int total = Integer.parseInt(getTextFirst) + Integer.parseInt(getTextSecond);
    String realTotal = String.valueOf(total);
    return realTotal;
}

由于您的 UI 组件是在创建第二个实例时从未调用过的方法中创建的,start因此两者都是textFirsttextSecondnull

首先修改您的处理程序以引用Calculator

class ButtonHandler implements EventHandler<ActionEvent>
{
    private Calculator calculator;

    public ButtonHandler(Calculator calculator) {
        this.calculator = calculator
    }

    public void handle(ActionEvent e)
    {
        this.calculator.calculation();
    }
}

这时候,行动起来,你可以简单地把所有calculation的实例都Calculator显示在屏幕上

然后,您将需要更新处理程序的创建...

myButton.setOnAction(new ButtonHandler(this));

最后,我会更新calculation以更新 UI,因为这些字段是private......

public void calculation()
{
    String getTextFirst = textFirst.getText();
    String getTextSecond = textSecond.getText();

    int total = Integer.parseInt(getTextFirst) + Integer.parseInt(getTextSecond);
    String realTotal = String.valueOf(total);

    textSum.setText(realTotal);
}

推荐阅读