-2

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?

Community
  • 1
  • 1
jobin
  • 2,600
  • 7
  • 32
  • 59
  • I never thought enhanced for loops would result in index counters. Note that this method will fail is your list is bigger than noOfJobs. – Hannes Jul 05 '14 at 12:12
  • @Hannes: I am pretty sure that the length of the list is equal to `noOfJobs`. However, is there a better method to make sure the method does not fail even if this is violated? – jobin Jul 05 '14 at 12:16
  • You can use `int length = Math.min(noOfJobs, numbersListAsString);` to be sure, then use the determined length to size the job array and iterate through the list i = 0; i < length. – Hannes Jul 05 '14 at 20:26

4 Answers4

2

You need to create an instance of the Job class before you can assign to its fields

    for (String s : numbersListAsStrings) {
        String[] jobWeightLength = s.split(" ");
        integerList[i] = new Job();
        // ...
Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
2

Try following loop instead the one you have.

for (String s : numbersListAsStrings) {
        String[] jobWeightLength = s.split(" ");
        integerList[i]=new Job();
        integerList[i].weight = Integer.parseInt(jobWeightLength[0]);
        integerList[i].length = Integer.parseInt(jobWeightLength[1]);
        i++;
    }

This should solve your problem.

Darshan Lila
  • 5,772
  • 2
  • 24
  • 34
  • Thanks! @IanRoberts answered a little earlier, +1 though- this was exactly what was wrong. – jobin Jul 05 '14 at 10:34
1

integerList[i] is null. integerList[i].weight references the weight field of a null Job. You should fill integerList with new Job() objects before using it.

see:

for (int i = 0; i < noOfJobs; i++)
    integerList[i] = new Job();
Unihedron
  • 10,902
  • 13
  • 62
  • 72
  • Thanks, @IanRoberts answered a little earlier, +1 though, this was exactly what was wrong. :) – jobin Jul 05 '14 at 10:35
0
public Job[] convertStringListToIntegerList(
        Integer noOfJobs, List<String> numbersListAsStrings) {
    int size = Math.min(noOfJobs, numbersListAsStrings.length());
    Job[] integerList = new Job[size];

    for (int i = 0; i < size; i++) {
        String[] jobWeightLength = numbersListAsStrings.get(i).split(" ");
        integerList[i] = new Job();
        integerList[i].weight = Integer.parseInt(jobWeightLength[0]);
        integerList[i].length = Integer.parseInt(jobWeightLength[1]);
    }
    return integerList;
}
Hannes
  • 2,018
  • 25
  • 32