This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing a method of an item of an array of objects

 
Stuart Jackson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Bye

Stefan
 
Stuart Jackson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stuart Jackson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic