1

I am getting a NumberFormatException error during runtime. The error is caused when checking if the JTextField is empty using an IF Statement.

    private void btnAddNumberMouseClicked(java.awt.event.MouseEvent evt) {                                          
    numberArray.add(Integer.parseInt(txtNumber.getText()));
    if(!(txtNumber.getText().equals(""))){
        scoresList.setText("");
        for (int i = 0; i < numberArray.size(); i++) {
            scoresList.append(Integer.toString(numberArray.get(i)) + "\n");
        }
    }
    txtNumber.setText("");
xenteros
  • 15,586
  • 12
  • 56
  • 91
Mos Def
  • 11
  • 1
  • 6
  • The first line parses the same value you then test for equality with `""`. Think about the order there. – Elliott Frisch Sep 12 '16 at 23:01
  • I placed the line where I parsed the value down into the IF statement just above the FOR loop and everything works perfectly. Thank you @ElliottFrisch :) – Mos Def Sep 13 '16 at 01:43
  • @MosDef I added the answer. Please add the stacktrace and tell me if my solution works. – xenteros Oct 12 '16 at 11:58
  • Possible duplicate of [java.lang.NumberFormatException: For input string: "23 "](http://stackoverflow.com/questions/28586430/java-lang-numberformatexception-for-input-string-23) – Pavneet_Singh Oct 12 '16 at 12:02
  • @MosDef If my answer has solved your problem, please mark it as a solution by clicking on a gray tick below the question score. – xenteros Oct 13 '16 at 04:45
  • Possible duplicate of [What is a NullPointerException and how can I fix it?](http://stackoverflow.com/questions/39849984/what-is-a-nullpointerexception-and-how-can-i-fix-it) – xenteros Oct 13 '16 at 05:49

2 Answers2

0

Might want to change this:

Integer.toString()

to something like this

String.valueOf()

Or you could just append the integer in the textArea without casting it to String.

Also try to use the .isEmpty() method instead of .equals()

0

The error is in the following line:

numberArray.add(Integer.parseInt(txtNumber.getText()));

Answering your code would be easier if you have added the stacktrace which is the error code you receive. It looks similar to the following:

Exception in thread "main" java.lang.NumberFormatException: For input string: "6 "
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)

The crucial part of the stacktrace in this case it the first line. It says what input caused the problem. The rest of the stacktrace says exactly what caused the Exception to be thrown.

If you update the question with the stacktrace, I'll fully answer your question. Now, let me guess what might help.

Your problem might be caused by three situations:

  1. You're trying to parse a null,
  2. Your String is lead or followed by white characters which you don't see or is empty,
  3. The String isn't parsable at all.

I believe it's the second option, because otherwise you would notice the problem from the stacktrace.

The solution might be:

numberArray.add(Integer.parseInt(
    (txtNumber.getText().trim().equals("") ?
       "0"
     :
       txtNumber.getText().trim()
));

if it still throws an Exception please give me a comment and update the question with a stacktrace.

xenteros
  • 15,586
  • 12
  • 56
  • 91