This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how do i create an array of objects

 
varsha rao
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
i have created an array or my own named Store. i want to create an array of objects using this class.
iv used
Store[] s=new Store[2];
then im assigning values to the elements using a for loop
for(int i=0;i<2;i++)
{ s[i].setXXX(parameter);}
but im gettin a null pointer exception here on s[i].setXXX line.
Please tell me how to correct this part. Where have i gone wrong.
 
Kj Reddy
Ranch Hand
Posts: 1704
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just declared Store[] s array object but did not initialized the s[1] & s[2] objects. You need initialize them before calling the set methods. How about modifying your for loop some thing similar to:


for(int i=0;i<2;i++)
{
s[i] = new Store(); // initializing
s[i].setXXX(parameter);
}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've declared an array of Store objects called s, and instantiated the array with a length of 2...

Store[] s = new Store[2];

But there are no Store objects referenced in the array. Instead, s[0] and s[1] are both null, which results in the NullPointerException when you attempt to dereference them.

You need to assign references to Store instances. For example...

s[i] = new Store();
 
varsha rao
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi every body
thanks for helping me out, i got it working
regards
varsha
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic