Ven Nilson
Ven Nilson

Reputation: 1019

How to handle exception on empty textfields?

I want to calculate logarithm values but error occurs on empty text fields.

//Libraries
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import java.awt.Font; //For Font And Size
import java.awt.Color; //For Color Change
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ChildClass extends JFrame {

    private JTextField textfield1;
    private JTextField textfield3;
    private JTextField textfield4;
    private JButton button1;

    public ChildClass() {
        super("Logrithm");
        getContentPane().setBackground(new Color(153, 204, 153));

        getContentPane().setLayout(null);

        textfield1 = new JTextField();
        textfield1.setBounds(62, 77, 62, 26);
        getContentPane().add(textfield1);

        textfield3 = new JTextField();
        textfield3.setBounds(127, 224, 153, 26);
        getContentPane().add(textfield3);

        textfield4 = new JTextField();
        textfield4.setBounds(175, 124, 62, 26);
        getContentPane().add(textfield4);

        button1 = new JButton();
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                double number1,baseE,result;
                String text1 = textfield1.getText();
                String text3 = textfield4.getText();

                if (text1.isEmpty() && text3.isEmpty()) {
                    JOptionPane.showMessageDialog(null, "Enter Values In TextField", "Invalid TextFields", JOptionPane.ERROR_MESSAGE);
                }

                if (text3.isEmpty()) {
                    number1 = Double.parseDouble(text1);
                    result = Math.log(number1);
                    textfield3.setText("" + result);
                }
                else if (!text3.isEmpty()) {
                    number1 = Double.parseDouble(text1);
                    baseE = Double.parseDouble(text3);
                    result = Math.log(number1) / Math.log(baseE);
                    textfield3.setText("" + result);
                }
            }
        });
        button1.setFont(new Font("Bullet", Font.PLAIN, 16));
        button1.setText("Click It");
        button1.setBounds(161, 172, 87, 26);
        getContentPane().add(button1);
    }
}

This is Main Method:

public class MainMethod {

    public static void main(String[] args) {

        ChildClass obj=new ChildClass();

        obj.setSize(450, 400);
        obj.setResizable(false);

        obj.setDefaultCloseOperation(obj.EXIT_ON_CLOSE);
        obj.setLocationRelativeTo(null);
        obj.setVisible(true);
    }
}

Problem I am facing when I click the jbutton with empty textfields. Popup MessageDialog comes after I click ok and then:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String

How to handle this error?

Upvotes: 0

Views: 6068

Answers (2)

Eranda
Eranda

Reputation: 1467

java.lang.NumberFormatException throws when you trying to parse a string to a Integer/Double/Long where the string does not match with the Number format you are parsing. Eg. Double.parseDouble("abc");

Here there are three places where it happens.

number1=Double.parseDouble(text1);

number1=Double.parseDouble(text1);

baseE=Double.parseDouble(text3);

This can happen if the user add a string which does not match double format.

Upvotes: 0

copeg
copeg

Reputation: 8348

problem i facing when click the jbutton with empty textfields. popup MessageDialog Come after i click ok and then Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String how to handle this error

If the value in the JTextArea is empty (or not valid - you may wish to include a check for valid numeric's, or use a JFormattedTextField)

  1. Warn the user
  2. Don't attempt to process the value (return from the method, or have the remaining code in the else clause)

For example:

//note the 'or' rather than 'and' below, presuming both must be valid
if( text1.isEmpty() || text3.isEmpty()){
    JOptionPane.showMessageDialog(null, "Enter Values In TextField", "Invalid TextFields", JOptionPane.ERROR_MESSAGE);
    return;//return from the method to allow the user to edit the JTextField
}

Upvotes: 2

Related Questions