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

getting NullPointerException

 
Rupesh Pradhan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I sincerely hope that this post fits in the beginners category because I *am* a beginner, whether or not the code looks like beginners stuff or not!

I am not sure if what I am saying is right, so correct me. "Can I use an array element as a handle for an object?"

I thinks the above sums up what I am trying to achieve. The code given below may be pretty long but it is well commented. Please help me out.

Thanks
Rupesh

 
Mark Patrick
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does declaration of an object array automatically initialize it's elements?



This line does not mean that Class8C is an array filled with new Result objects. It means that Class8C is a new array object that can hold Result objects, however, each element is null. You have to assign each element an actual Result object.
 
Rupesh Pradhan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I request you to please help me out by providing a few lines of code on how to "assign each element an actual Result object." in this program.

Rupesh
 
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
Here you (perhaps incorrectly -- more in a minute) assign a number to each element of an array:


You just have to do the same thing, but using "new" to create a Result object for each element; you've already shown in a comment how to create a single Result, you just have to do it the same way and assign them to the array elements. I don't want to simply write the code, as puzzling it out yourself has some benefits, and you're already quite close.

As for the "perhaps" incorrect part: note that the first element of a Java array is element 0, but this loop only assigns values to elements 1, 2, 3, 4 (because 4 is less than 5.) You actually want the lower limit of your loop to be at 0, not 1, so that all 5 values get assigned.
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rupesh Pradhan:
Can I request you to please help me out by providing a few lines of code on how to "assign each element an actual Result object." in this program.

Rupesh




The index of the first element in an array is 0. Check out the Sun Tutorial: Arrays for more info.
 
Rupesh Pradhan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does the trick.

Thanks a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic