-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
  • 118,630
  • 17
  • 138
  • 146

1 Answers1

0

You just need to initialize your strings

ArrayList<String> strings = new ArrayList<String>();
Liu
  • 970
  • 7
  • 19