For input string: “” when textfields are filled out


I am writing a program in Java where I got some textfields and a button.

I get a java.lang.NumberFormatException: For input string: "" even though I have filled out all the textfields when running the program.

My code looks something like this:

button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { method(); } } ); public void method() { try { String string1 = textfield1.getText(); String string2 = textfield2.getText(); String string3 = textfield3.getText(); if ( string1.length() == 0 || string2.length() == 0 || string3.length() == 0) { System.out.println("fill in the required text fields"); return; } int number = Integer.parseInt(textfield3.getText()); //do something } catch ( NumberFormatException e ) { System.out.println("Wrong format"); } }


<a href="http://pastie.org/7824509" rel="nofollow">See more code here</a>


I have tested your program a little bit and you have a problem with the text field, because of the creation of your panel and switching which one is active.

In the constructor you call the something() method which creates the JTextField. When the button is clicked you call again something() and a new JTextField is generated which you also add to the panel.

So you have two JTextFields on the GUI at the exact same position but only a reference to one of them (the last one created).

When you click the button which will call method(). The hidden TextField is asked for his text (this is how it works on my pc) and this is always empty because I can only write into the one I see.

An easy fix to this is to change the method actionPerformed:

@Override public void actionPerformed( ActionEvent e ) { if ( e.getSource() == button1 ) { present = something; button1.setVisible(false); //something(); visiblePanel(); previous = something; } }

So I avoid the new creation of the JTextField but visiblePanel() ensures the TextField and second button are shown.

After this change I can type in "sadda" press the button and see the output "Numberformatexception". When I type in a number I see nothing so the formatting works.


  • Add image to JPanel within JLabel
  • Clear current FocusOwner (jTextfield)
  • Does NSUserDefaults save objects if the application becomes inactive?
  • Rails updating attributes of a User Model from OrdersController
  • UITextfield not editable-iphone [duplicate]
  • PHP html_entity_decode Doesn't Decode Entity as Expected?
  • Jackson Parser can't read backslash quotation marks in String
  • Automatic web-page refresh memory leak using XMLHttpRequest
  • How to make a Node from a String?
  • How to select multiple rows at different place in a table using Selenium Webdriver
  • How to compare Strings for Percentage Match using vb.net?
  • Select multiple items in JTextPane
  • Why is this code not working? Hangman
  • Insert audio album in Android MediaStore
  • Send data from edittext to listview
  • How to getText() from the input field of an angularjs Application
  • Reading a file into a multidimensional array
  • Does Mobilefirst provide a provision to access web services directly?
  • App restarts from wrong activity
  • Jetty Server not starting: Unable to establish loopback connection
  • Retrieving value from sql ExecuteScalar()
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • MySQL WHERE-condition in procedure ignored
  • sending/ receiving email in Java
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Web-crawler for facebook in python
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Matrix multiplication with MKL
  • retrieve vertices with no linked edge in arangodb
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • trying to dynamically update Highchart column chart but series undefined
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • apache spark aggregate function using min value
  • Does armcc optimizes non-volatile variables with -O0?
  • java string with new operator and a literal