APerson
APerson

Reputation: 712

getText() Method returns null when there is text

Whenever I enter a number in my text box and get the text and convert it I get an exception:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""

Here is the code for the class:

Number Generator:

package Main;

public class Number_Generator {

    public int makeRandom(){
        int Rnumber = 1;
        Rnumber = 1 + (int)(Math.random()*20);
        return Rnumber;
    }



}

Oper_add:

package Main.Oper_classes;
import Main.Number_Generator;
import javax.swing.*;
import java.awt.*;
import Main.Oper_check.*;

public class Oper_add {

    Number_Generator gen = new Number_Generator();
    public JTextField textbox = new JTextField(2);
    //make random numbers
            public Integer Rnumber1 = gen.makeRandom();
            public Integer Rnumber2 = gen.makeRandom();


    public void main(){
        //set textbox to null
        this.textbox.setText(null);

        //import classes
        Add_Check check = new Add_Check();
        //convert to String
        String Rnumber1S = Rnumber1.toString();
        String Rnumber2S = Rnumber2.toString();

        //make GUI
        JFrame frame = new JFrame("Addition Test");
        JPanel panel = new JPanel();
        FlowLayout fl = new FlowLayout();
        panel.setLayout(fl);

        JLabel num1 = new JLabel(Rnumber1S);
        JLabel sign = new JLabel("+");
        JLabel num2 = new JLabel(Rnumber2S);
        JLabel equals = new JLabel("=");
        JButton confirm = new JButton("Check");

        //add action listener
        confirm.addActionListener(check);

        //add componets to window
        panel.add(num1);
        panel.add(sign);
        panel.add(num2);
        panel.add(equals);
        panel.add(textbox);
        panel.add(confirm);

        //set frame settings
        frame.setContentPane(panel);
        frame.setVisible(true);
        frame.pack();

    }

}

add_check:

package Main.Oper_check;
import java.awt.event.*;
import javax.swing.JOptionPane;
import Main.Oper_classes.*;


public class Add_Check implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        Oper_add add = new Oper_add();

        String textbox = add.textbox.getText().trim();
        Integer textboxC = Integer.parseInt(textbox);
        Integer total = add.Rnumber1 + add.Rnumber2;


    }

}

Upvotes: 0

Views: 7758

Answers (2)

Aurand
Aurand

Reputation: 5537

Oper_add add = new Oper_add();
String textbox = add.textbox.getText().trim();
Integer textNumber = Integer.parseInt(textbox);

You are creating a new Oper_add which has a new, empty text box in it. You're expecting that text box to have a value, but it won't. It is in no way related to whatever text box you're interacting with.

Upvotes: 2

John Tang Boyland
John Tang Boyland

Reputation: 1088

The text box currently has an empty string (or maybe just spaces -- trim removes spaces). It doesn't have null. The exception is because the empty string is not a valid number. You should catch the exception and then use it in an JOptionPane:

try {
    Integer textNumber = Integer.parseInt(textbox);
     ...
} catch (NumberFormatException ex) {
    JOptionPane.showMessageDialog(this, ex.getMessage(), "Number Error", JOptionPane.ERROR_MESSAGE);
}

Upvotes: 1

Related Questions