Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot access class attribute

 
Paul Burgess
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch Paul
 
Paul Burgess
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic