Reputation: 11
My application is intended to read an existing text file, line for line, of exactly 1500 items into an array of item class objects. The goal is to get the data into the array so I can use this application as a starting point for converting the archive for a new program I am writing.
package sandboxPackage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class mainClass {
public static void main(String[]args) throws FileNotFoundException, IOException {
InputStream in = new FileInputStream(new File("C:\\Documents and Settings\\Adam\\Desktop\\Cloud Project\\MasterIndex.library"));
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder out = new StringBuilder();
String line;
itemClass[] m = new itemClass[1500];
int i = 0;
while ((line = reader.readLine()) != null) {
m[i].index = line; // crash is here
m[i].location = reader.readLine();
m[i].item = reader.readLine();
m[i].description = reader.readLine();
i++;
}
//Print the entire list
for (i = 0; i == 1499; i++) {
System.out.println(m[i].index);
System.out.println(m[i].location);
System.out.println(m[i].item);
System.out.println(m[i].description);
//System.out.println("This is item #" + i + 1);
}
}
}
And here is the itemClass:
package sandboxPackage;
public class itemClass{
String index;
String item;
String description;
String location;
}
The text file looks like this: Index Location Item Description Index Location Item Description Index ..
The compiler claims the NullPointerException is on line 20, which is the first line of the while loop, but I just don't see it. I have looked at about a thousand other examples of this same error but it still does not compute for me.
Upvotes: 0
Views: 204
Reputation: 68715
You are just declaring an array of objects:
itemClass[] m = new itemClass[1500];
but you never instantiate the objects within this array. So accessing any instance variable in will throw a NullPointerException
Add instantiation of array objects in your loop:
while ((line = reader.readLine()) != null) {
m[i] = new itemClass();// change the constructor if u need to
m[i].index = line; // crash is here : should no more crash
m[i].location = reader.readLine();
m[i].item = reader.readLine();
m[i].description = reader.readLine();
i++;
}
Upvotes: 6
Reputation: 301
You initialize the array of itemClass
yes, but you never populated it with itemClass
objects. First you have to create an itemClass
before assigning it's attributes. Try this.
while ((line = reader.readLine()) != null) {
itemClass item = new itemClass();
item.index = line;
item.location = reader.readLine();
item.item = reader.readLine();
item.description = reader.readLine();
m[i] = item;
i++;
}
Upvotes: 0
Reputation: 159864
Elements in an Object type array are null
by default. Initialise the elements before attempting to assign values to their fields
while ((line = reader.readLine()) != null) {
m[i] = new ItemClass();
...
}
Upvotes: 2