0

For some reason the AddListener class below doesn't work, and I keep getting a number format exception. Could anyone please tell me the reason for this. It seems to me as if it should work.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class BabyCalculator extends JFrame {

    JFrame theFrame = this;
    JTextField addField; // Declaring this here so that you can access the variable from other places. MAKE SURE TO NOT DECLARE THIS AGAIN IN THE CONSTRUCTOR
    JTextField totalField;

    public BabyCalculator() {
        //You set this up so that you can refer to the frame using the inner class below.
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setName("Baby Calculator");
        setLayout(new GridLayout(3, 0));
        //add
        JLabel addLabel = new JLabel("Amount to add:");
        addField = new JTextField(10);
        JButton addButton = new JButton("add");
        addButton.addActionListener(new AddListener());
        //multiply
        JLabel multiplyLabel = new JLabel("Amount to multiply:");
        JTextField multiplyField = new JTextField(10);
        JButton multiplyButton = new JButton("multiply");
        //total
        JLabel totalLabel = new JLabel("Total");
        totalField = new JTextField(10);
        totalField.setEditable(false);
        JButton stopButton = new JButton("Stop");
        stopButton.addActionListener(new StopListener());
        //Create Panels
        JPanel topRow = new JPanel(new BorderLayout());
        JPanel middleRow = new JPanel(new BorderLayout());
        JPanel bottomRow = new JPanel(new FlowLayout());
        //Add the top Row
        topRow.add(addLabel, BorderLayout.WEST);
        topRow.add(addField, BorderLayout.CENTER);
        topRow.add(addButton, BorderLayout.EAST);
        add(topRow);

        middleRow.add(multiplyLabel, BorderLayout.WEST);
        middleRow.add(multiplyField, BorderLayout.CENTER);
        middleRow.add(multiplyButton, BorderLayout.EAST);
        add(middleRow);

        bottomRow.add(totalLabel);
        bottomRow.add(totalField);
        bottomRow.add(stopButton);

        add(bottomRow);


        pack();
        setVisible(true);
    }


    public class AddListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String addFieldText = addField.getText();
            String totalFieldText = totalField.getText();
            double addAmount = Double.parseDouble(addFieldText);
            double total = Double.parseDouble(totalFieldText);
            total += addAmount;
            totalField.setText(total + "");

        }
    }
    //end class AddListener

    public class StopListener implements ActionListener {//this is an inner class

        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(theFrame, "You Clicked the stop button");
        }//end class StopListener
    }

    public static void main(String[] args) {
        JFrame newFrame = new BabyCalculator();
    }
}

Note: The problem in the code is definitely related to the AddListener. Whenever I click the add button in the GUI I get an exception.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
aaa
  • 99
  • 1
  • 1
  • 7
  • Look at the number format of text fields? What number you entered at text fields? – Masudul Mar 23 '15 at 04:32
  • and what line in your code throws this exception? – Ouerghi Yassine Mar 23 '15 at 04:32
  • "Whenever I click the add button in the GUI I get an exception" - Have you typed valid numbers into _both_ your fields before clicking on the button? Your code will throw an exception if either field is empty or contains a non-numeric value. – aroth Mar 23 '15 at 04:32
  • You need to proper handle it because use can input any string that is not number. – Braj Mar 23 '15 at 04:33
  • Must read [Is there any way to accept only numeric values in a JTextField?](http://stackoverflow.com/questions/1313390/is-there-any-way-to-accept-only-numeric-values-in-a-jtextfield) – Braj Mar 23 '15 at 04:34
  • Consider using a `JSpinner` with a [`SpinnerNumberModel`](https://docs.oracle.com/javase/8/docs/api/javax/swing/SpinnerNumberModel.html) instead. – Andrew Thompson Mar 23 '15 at 04:39

1 Answers1

1

The problem is with the totalFieldText, it's default value is blank, meaning that when you try and convert to a double value, it causes a NumberFormatException

Try giving it a default value of 0, for example

totalField = new JTextField("0", 10);

You might also like to take a look at How to Use Spinners and How to Use Formatted Text Fields which will make your life easier

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366