wood burning stoves 2.0*
The moose likes Java in General and the fly likes Cannot access class attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Cannot access class attribute" Watch "Cannot access class attribute" New topic
Author

Cannot access class attribute

Paul Burgess
Greenhorn

Joined: Aug 29, 2010
Posts: 3
Hi everyone,

This problem is starting to drive me insane. I'm sure it must be something really simple, but I just can't seem to find out where the problem is. I initialize a population from the main() function, containing a collection of individuals with:

I then try to access the attributes of one of those Individuals to test if the program works with:

But it does not work, the compiler throws a null pointer exception. Any help anyone can provide will be gratefully accepted.


Cheers,
Paul











Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

In your Population class you're creating an Individual array, but you aren't putting anything in it. When an array of some sort of object is created it just contains nulls everywhere. So when you call robots[0].getPositionX() ...robots[0] is null.

You need to create the Individual objects and put them in the array.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to JavaRanch Paul
Paul Burgess
Greenhorn

Joined: Aug 29, 2010
Posts: 3
Hi both,

Many many thanks for your reply Matthew, I had indeed not created the array members. And thanks also for your welcome to the forum Seetharaman.

Cheers,
Paul
Paul Burgess
Greenhorn

Joined: Aug 29, 2010
Posts: 3
Hi,

I should have posted the code that worked with that last post, just in case others with the same issue stumble across this thread; I had to rewrite the constructor for the Population class:



Cheers,
Paul
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot access class attribute
 
Similar Threads
android gridview with fling + flipper
audio visualization graphics
bouncy ball applet image problem
Ball Physics implementation.
Animation