0

When converting a String to an int using the following code, I get the error

Exception in thread "main" java.lang.NumberFormatException: For input string: "null1"

Here is the code (well, the line the error occurs):

int numbProgram=
  Math.abs(Integer.parseInt(standardProgramResult) - output[0])
  + Math.abs(Integer.parseInt(standardProgramResult) - output[1])
  + Math.abs(Integer.parseInt(standardProgramResult) - output[2])
  + Math.abs(Integer.parseInt(standardProgramResult) - output[3])
  + Math.abs(Integer.parseInt(standardProgramResult) - output[4])
  + Math.abs(Integer.parseInt(standardProgramResult) - output[5])
  + Math.abs(Integer.parseInt(standardProgramResult) - output[6])
  + Math.abs(Integer.parseInt(standardProgramResult) - output[7]);

So what does null1 mean? Shouldn't that just mean 1 because null means nothing? And also, how can I fix this?

Thanks

5
  • 1
    Are you sure it is null1 and not null? Anyway to get better help post SSCCE and entire stacktrace. Commented Jun 26, 2015 at 21:46
  • 2
    What is the value of standardProgramResult? And why are you calling parseInt() so many times? Do you know what a for loop is? Commented Jun 26, 2015 at 21:46
  • 3
    standardProgramResult is probably not a proper int - so it throws an error while trying to parse it. Commented Jun 26, 2015 at 21:48
  • Please include the code that defines standardProgramResult. Commented Jun 26, 2015 at 21:49
  • 1
    Then it means that standardProgramResult is "null1" which is not proper integer and it can't be parsed to int so you are getting NumberFormatException. If you ware expecting standardProgramResult to have other value than null1 then problem lies in code which you are not showing us so we can't help you much. For now it looks like it was result of concatenating null with numeric String (in this case "1"). Commented Jun 26, 2015 at 21:57

2 Answers 2

3

First of all you can parse only one time and use a loop:

int programResult = Integer.parseInt(standardProgramResult);
int numbProgram=0;
for (int output: output){
  numProgram += Math.abs(programResult - output)
}

That said, standardProgramResult does not contain an integer value and can not be parsed. The exception is showing that.

Somewhere in your code you probably have something like:

standardProgramResult = someVar1 + someVar2;

And someVar1 is "null".

To better understand and handle this use exceptions:

int programResult = 0;
try {
    programResult = Integer.parseInt(standardProgramResult);
} catch (NumberFormatException e) {
    System.err.println("programResult was not a number: " + programResult);
    // possibly ignore error, or terminate...
    // e.printStackTrace(); // prints the stack trace
    // throw e; // throws the error for someone else to handle
    // System.exit(1); // terminate indicating an error in execution
}
int numbProgram=0;
for (int output: output){
    numProgram += Math.abs(programResult - output)
}
Sign up to request clarification or add additional context in comments.

10 Comments

Alternatively, the for loop can be written as for (int output: output). I'm not sure which is better, though...
Oh, found my problem. I was starting a new process and the standard output for that was null. I realized that because you said that I added up to variables in it and one of them was null. Thanks
@APCoding glad to help, you should add exception handling, you may want to ignore the error, or terminate in a more polite way :)
Your try-catch example is broken; the message in the NumberFormatException already contains the unparsed text, and furthermore Math.abs() doesn't raise a NumberFormatException.
@dimo414 ahh thanks, I mean well... I gave an example, he may not want to print the error at all.
|
-1

The problem is that standardProgramResult is null. You need to use a debugger to find out why.

I have two suggestions which don't directly answer your question, but will help you figure out what the problem is:

  1. Assign the value of parseInt() to a variable so you can reuse the result as many times as you wish:

    int result = Integer.parseInt(standardProgramResult);
    

    In general, you shouldn't do too much in a single line of code as it makes it more difficult to track down errors.

  2. Use a for loop whenver you have an array of values and need to repeat the same task for each value in the array.

5 Comments

standardProgramResult is not null, it would raise a NullPointerException if it were. It is the string "null1".
@dimo414 The documentation for parseInt() mentions nothing about throwing an NPE. I'm certain that I have seen it throw a NumberFormatException for null input, which would certainly conform to the documented behavior. Assuming that the "null1" isn't a typo, you are probably correct that the String isn't actually null.
Yes, apparently it raises a NumberFormatException for null. Nevertheless, "null1" is clearly not a typo, the error message for null is simply "null".
@dimo414 Thus the possibility of a typo by accidentally adding an extraneous 1.
No. The message in OP's post is For input string: "null1". This is clearly different from null.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.