-
March 31st, 2011, 04:53 PM
#1
Error Message
Hi. I am working on a very simple program that should add two number in two JTextField boxes and when I try to parse from JTextField to Integer and I try to run the program there is this error message.
==================================================================
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.parseInt(Integer.java:499)
at TextFieldFrame.<init>(TextFieldFrame.java:17)
at TextFieldTest.main(TextFieldTest.java:5)
Press any key to continue . . .
==================================================================
The two files are these:
------------------------------------------------------------------------------------------------------------------------
import javax.swing.*;
public class TextFieldTest{
public static void main(String[] args){
TextFieldFrame textFieldFrame = new TextFieldFrame();
textFieldFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textFieldFrame.setSize(325, 100);
textFieldFrame.setVisible(true);
}
}
--------------------------------------------------------------------------------------------------------------------
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import javax.swing.JButton;
public class TextFieldFrame extends JFrame{
private JTextField textField1;
private JTextField textField2;
private JButton Convert;
public TextFieldFrame(){
super();
setLayout( new FlowLayout());
textField1 = new JTextField(10);
int num1 = Integer.parseInt(textField1.getText());
add(textField1);
textField2 = new JTextField(10);
int num2 = Integer.parseInt(textField2.getText());
add(textField2);
Convert = new JButton("Convert");
add(Convert);
}
}
-
April 1st, 2011, 04:01 AM
#2
Re: Error Message
I guess you wanted to create the JTextFields with an integer value s their text content, but if you had read the API docs first you would know that the constructor that takes an int argument
Constructs a new empty TextField with the specified number of columns
You should have used this instead:
Code:
textField1 = new JTextField("10");
When posting code use code tags.
-
April 1st, 2011, 01:14 PM
#3
Re: Error Message
The reason you are getting an exception is as jcaccia has stated ie you aren't initialising the JTextFields with default values of 10 you are making JTextFields with 10 columns. When you then call getText() there is no value to return, just an empty string, and you can't convert an empty string to an integer.
BTW what is the point of your program, it doesn't give you the opportunity to enter values into the text fields it just uses the default value.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|