Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

getting NullPointerException

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That does the trick.

Thanks a lot.
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic