I've been trying to read from a csv file and fill an array of State objects, but the array never gets filled past the 1st ([0]) index. The format of the csv file is like this:
Mississippi,Jackson,MS,2991207,South,4
New Hampshire,Concord,NH,1323459,New England,2
where each set of data is separated by a new line.
inFile.useDelimiter(",|\n");
String headerLine = inFile.nextLine();
for(int i = 0; i<50; i++) {
while(inFile.hasNext()) {
State state = new State(inFile.next(), inFile.next(), inFile.next(), inFile.nextInt(),
inFile.next(), inFile.nextInt());
stateArray[i] = state;
}
}
This is my code for populating the array of States with elements. It uses a Scanner called inFile to parse the csv. I'm not sure how else to do this. I've tested with print statements, and only the first index of the stateArray ever has any fields filled in it.
System.out.println(stateArray[0].GetStateName());
for example, returns "Mississippi", but any other index results in a Null Pointer Exception.