I've been looking at answers to other posts and they seem to point to what I'm doing, but I'm still getting NumberFormatException
. I'm trying to build a simple calculator from a JFrame
by pulling jtextfield value and converting it into int, but I'm getting the following exception.
package com.calculator.app;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class CalculatorApp extends JFrame {
private JPanel contentPane;
private JTextField textField = new JTextField();
private JTextField textField_1 = new JTextField();
private JTextField textField_2 = new JTextField();
int num1 = Integer.parseInt(textField.getText());
int num2 = Integer.parseInt(textField_1.getText());
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CalculatorApp frame = new CalculatorApp();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CalculatorApp() {
// int num2 = Integer.parseInt(value2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblCalculatorApp = new JLabel("Calculator App");
lblCalculatorApp.setBounds(141, 37, 138, 23);
contentPane.add(lblCalculatorApp);
JButton btnNewButton = new JButton("Add");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnNewButton.setBounds(74, 112, 131, 31);
contentPane.add(btnNewButton);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setBounds(169, 181, 82, 23);
contentPane.add(lblNewLabel);
textField = new JTextField();
textField.setBounds(55, 73, 54, 23);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(151, 70, 54, 23);
contentPane.add(textField_1);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(109, 162, 124, 55);
contentPane.add(textField_2);
// textField_2.setText(String.valueOf(output));
try{
int output = Integer.parseInt(textField.getText());
}
catch(NumberFormatException e){
System.out.println("Error");
}
}
}
Exception
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.calculator.ap.CalculatorApp.<init>(CalculatorApp.java:23)
at com.calculator.ap.CalculatorApp$1.run(CalculatorApp.java:32)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
This line:
is equivalent to
because you don't initialize the
textField
with a specific value in this line:So, you are trying to parse an empty string as integer, but it throws
NumberFormatException
, because it's not an integer.If you just want to get over those problems, you could put 3 integers there:
or (much better) you can remove the following lines, because it seems that you need to parse them later (when the button is clicked):
The explanation above helps you to avoid that exception.
But if you want to sum those numbers, you should fill in the body of this method:
where
getIntegerValue
can look like this:or shorter:
This method:
0
if the string is empty (no value)NumberFormatException
if the string is not an integer or an empty stringNumberFormatException
, because it's aRuntimeException
Of course, you can remove that
if
/ ternary operator from the last method if you want to throw an exception even if the string is empty.