I am trying to convert a String to an Integer using the Integer.parseInt()
method as follows:
public Job[] convertStringListToIntegerList(
Integer noOfJobs, List<String> numbersListAsStrings) {
Job[] integerList = new Job[noOfJobs];
int i = 0;
for (String s : numbersListAsStrings) {
String[] jobWeightLength = s.split(" ");
integerList[i].weight = Integer.parseInt(jobWeightLength[0]);
integerList[i].length = Integer.parseInt(jobWeightLength[1]);
i++;
}
return integerList;
}
Here Job
is defined as follows:
public class Job {
Integer length;
Integer weight;
Integer difference;
Float ratio;
}
I have referred to this question:
However, as you can see the variable I am using to store the result of Integer.parseInt()
is an Integer
, not an int
and yet getting a NullPointerException
at:
integerList[i].weight = Integer.parseInt(jobWeightLength[0]);
Can you please point me as to what's going wrong here?