I have two classes, one of which is the main class that is to run the program (modchoice), and a second that is for storing the data (modules). There is a private array of modules in the modchoice class declared as:
private modules themods = new modules;
The first thing the main function of the modchoice class does, is call a method for each element to set the array up:
This compiles, but when I try to run it, I get the error
java.lang.NullPointerException at modchoice.initArray(modchoice.java:54) at modchoice.<init>(modchoice.java:40) at modchoice.main(modchoice.java:34) Exception in thread "main"
This means nothing to me. Something about null pointers (although I thought java didn't use pointers ). I'm sure its some simple mistake but i've spent hours playing with it trying to work it out and would appriciate any help.
Thanks Stuart [ May 31, 2004: Message edited by: Stuart Jackson ]
When you allocate an array of class type using "new", what you get is an array of null references to objects. You still have to actually create the objects, too. You might do it in "initArray()" like this:
Now, let me give you some free advice. First of all, Java class names should always start with a capital letter; if they don't, it looks really weird, and leads to people having problems reading your code. "Modules" and "ModChoice" would be much better names.
Second, what you're doing in the method "setAll()" should instead be done in the constructor -- i.e., your constructor should take an argument list and initialize things, and setAll() should be eliminated. The way you have it set up, "modules" objects are "stillborn" -- when they're first created, they're kind of dead and not useable. [ May 31, 2004: Message edited by: Ernest Friedman-Hill ]