-2

I'm trying to make a simple calculator with a graphical user interface; however, I'm having trouble adding two values from the text fields. Each time I try and calculate the values, I receive an exception. Could someone help me out with this? I'm fairly new to java, and I'm open to feedback and suggestions. The exception is down below.

Thank you,

Kyle

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

Simple Calculator Picture

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)
Kyle
  • 23
  • 4
  • 3
    Do you really need to create a new instance of `Calculator` in the action handler? – MadProgrammer Aug 14 '18 at 01:54
  • 2
    you should share what the exception is – Andy Aug 14 '18 at 01:54
  • 1
    Questions seeking debugging help ("Causing Exception") must include the desired behavior, a **specific** problem or error and the **shortest** code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [mcve] – Patrick Parker Aug 14 '18 at 01:58

1 Answers1

0

To start with, your ButtonHandler is creating a new instance of Calculator which has nothing to do with the instance of Calculator which is currently displayed on the screen

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

        myButton.setText("Calculating");

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

This would mean that when calculation is called...

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;
}

Since your UI components are created in the start method, which is never called when you create the second instance, both textFirst and textSecond are null

Start by modifying your handler to take a reference to the Calculator

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

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

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

This when, actioned, you can simply all the calculation of the instance of Calculator which is actually on the screen

You will then need to update the creation of the handler...

myButton.setOnAction(new ButtonHandler(this));

And finally, I would update the calculation to update the UI, as the fields are 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);
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • *"`textFirst.getText()` and `textSecond.getText()` will either return null or an empty String, which is likely the cause of your exception"* Nope, afaik `getText` never returns `null`, but a empty string which would result in a `NumberFormatException` not a NPE. For the new `Calculator` instance `textFirst` and `textSecond` are not initialized. – fabian Aug 14 '18 at 10:33
  • @fabian Yes, but when I answered, all I had was "an exception", so it was a little but of guess work - There updated – MadProgrammer Aug 14 '18 at 11:07