File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Accessing a method of an item of an array of objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing a method of an item of an array of objects" Watch "Accessing a method of an item of an array of objects" New topic
Author

Accessing a method of an item of an array of objects

Stuart Jackson
Greenhorn

Joined: May 31, 2004
Posts: 3
Hi

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[3];

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 ]
Stefan Krompass
Ranch Hand

Joined: Apr 29, 2004
Posts: 75
Hi,

could you give us a minimal sample that shows your problem?

Bye

Stefan
Stuart Jackson
Greenhorn

Joined: May 31, 2004
Posts: 3
Yes, this is a cutdown version of the classes that produces the error:


The modchoice class that is executed:





The modules class that stores the data:

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

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 ]

[Jess in Action][AskingGoodQuestions]
Stuart Jackson
Greenhorn

Joined: May 31, 2004
Posts: 3
Thanks very much, worked a treat. I've corrected the classnames. I know I should have used a constructor, but I didn't know how to pass all the array elements with all the attributes of each item.
 
Don't get me started about those stupid light bulbs.
 
subject: Accessing a method of an item of an array of objects