This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes getting NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getting NullPointerException" Watch "getting NullPointerException" New topic
Author

getting NullPointerException

Rupesh Pradhan
Greenhorn

Joined: Nov 13, 2004
Posts: 3
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

Joined: Feb 22, 2004
Posts: 51
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.


Mark Patrick<br />SCJP 1.4
Rupesh Pradhan
Greenhorn

Joined: Nov 13, 2004
Posts: 3
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
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

Joined: Nov 13, 2004
Posts: 3
That does the trick.

Thanks a lot.
 
Consider Paul's rocket mass heater.
 
subject: getting NullPointerException
 
Similar Threads
concurrency error
Multi dimensional arrays
random access files
Arrays: Calling input from other methods and classes
Arrays in Hashtable