0

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.

Randall Cook
  • 6,728
  • 6
  • 33
  • 68
Starf1337
  • 11
  • 1
  • which line is "line 20"? – Hovercraft Full Of Eels Jul 16 '14 at 22:10
  • The compiler is definitely not complaining about a NullPointerException. Your IDE may have a static analysis tool that's warning about a possible NPE, but exceptions get thrown at runtime, not compile-time. – yshavit Jul 16 '14 at 22:10
  • 3
    You create an array of objects but fill it with nothing and then try to use it! It's like taking an empty carton of eggs (the array), and trying to make an omelette -- won't work! I suggest that you fill the array with new ItemClass instances before trying to access them. – Hovercraft Full Of Eels Jul 16 '14 at 22:10
  • First line of the while loop. – Starf1337 Jul 16 '14 at 22:11
  • 4
    NullPointerException is one of the *easiest* exceptions to debug. You know precisely what line was involved, and there are rarely more than 2-3 pointers on the line. One of them is null. Use a debugger or System.out.println to figure out which one. Then work backwards from there to figure out why. – Hot Licks Jul 16 '14 at 22:11
  • The stream works. There is data there. The file exists. I can get the data into the line string but ... – Starf1337 Jul 16 '14 at 22:12
  • @Starf1337: please re-read my comment. The array is filled with ***nulls***. First fill it with objects. – Hovercraft Full Of Eels Jul 16 '14 at 22:13
  • "The compiler claims the NullPointerException is on line 20". First off, you need to understand the difference between a compiler error and a runtime exception. I suspect that you're speaking of a runtime exception (but we don't know for sure because you didn't copy the *exact and complete* error message into your question as you should have). – Hot Licks Jul 16 '14 at 22:14
  • I don't want to insta-re-close as a dupe with my 20k power, but is this a dupe of http://stackoverflow.com/questions/16323030/nullpointerexception-in-array ? – yshavit Jul 16 '14 at 22:15
  • @Starf1337 "*In my mind the array initialization took care of every instantiation.*" and how on earth JVM would know which constructor should be used and what data to put in it :) ? – Pshemo Jul 16 '14 at 22:19
  • Apart from the otherwise correct answers, this solution screams for an `ArrayList` or `LinkedList` instead of an array of 1500 items. Then you could use `items.add(item)` to grow the list and you would never encountered the null pointer. – Maarten Bodewes Jul 16 '14 at 22:24
  • haha yeah that's just it, my brain was not working properly. I knew posting this question would incite a riot but all of the other NPE questions were for such obvious reasons like String s = null;. Even my sleep deprived brain can see what is wrong with that! So take it easy @HotLicks! – Starf1337 Jul 16 '14 at 22:29
  • 1
    By the way, the `for (i = 0; i == 1499; i++)` loop is broken. It will run zero times. The for condition is *while*, not *until*. You want to say `i < 1500`. – David Conrad Jul 16 '14 at 22:31

3 Answers3

6

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++;
}
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
2

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();
    ...
}
Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

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++;
}
JustWannaFly
  • 301
  • 1
  • 11