CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Error Message

  1. #1
    Join Date
    Mar 2011
    Posts
    0

    Exclamation 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);
    }
    }

  2. #2
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    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.

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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 code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured