-3
contactField = new JTextField();     
contactField.setBounds(165, 336, 100, 30);
contactField.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar(); 

if(!((c >= '0') && (c <= '9') || (c ==
KeyEvent.VK_BACK_SPACE) ||(c == KeyEvent.VK_DELETE))) {                       
e.consume();
}
} 
});
int contactvalue=Integer.parseInt(contactField.getText());

By running this code I am getting:

java.lang.NumberFormatException:Forinput string: ""
halfer
  • 19,824
  • 17
  • 99
  • 186
Shashi Dhar
  • 603
  • 6
  • 7
  • look for `contactField.getText()` – Ravi Apr 02 '17 at 10:39
  • `contactField.getText()` doesn't return a number format. – Mustafa Çil Apr 02 '17 at 10:40
  • 1) Why is a `contactField` numeric? 2) Try `JSpinner giveNumber = new JSpinner(new SpinnerNumberModel(20,1,1000, 1));` – Andrew Thompson Apr 02 '17 at 10:42
  • More generally: 1) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. .. – Andrew Thompson Apr 02 '17 at 10:45
  • .. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 4) For Swing, we typically use [key bindings](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) rather than the lower level `KeyListener`. Or for a `JTextComponent`, look to using a `DocumentListener`. – Andrew Thompson Apr 02 '17 at 10:46
  • BTW - is the line `int contactvalue=Integer.parseInt(contactField.getText());` outside or inside the `keyTyped(KeyEvent e)` method? From that dog's breakfast, I cannot tell. – Andrew Thompson Apr 02 '17 at 10:53

1 Answers1

1

The msg is descriptive:

java.lang.NumberFormatException:Forinput string: ""

Integer.parseInt(contactField.getText());

is throwing the exception because contactField is empty..

you just can not convert an empty string into an integer.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97