-4

Why do I get a NullPointerException when I try this:

ArrayList<String> longsToStrings() throws IOException {
    LinkedList<Long> longNumbers = populateArrayWithLongs();
    ArrayList<String> strings = null;
    for (Long l : longNumbers) {
        strings.add(l.toString());
    }
    return strings;
}

When I try to print the results to console, I get NullPointerException. populateArrayWithLongs() function returns array with longs: [1488758722000, 1488773070000, 1488787530000, 1488801890000]

Sean Bright
  • 114,945
  • 17
  • 134
  • 143

1 Answers1

0

You just need to initialize your strings

ArrayList<String> strings = new ArrayList<String>();
Liu
  • 944
  • 8
  • 18