Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Getting Null pointer while setting array of objects

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All, I am getting Null Pointer error while trying to set an array of objects for the following getter setter class.
I am relatively new to Java, can someone help me to find out the right way to do this.

Thanks,
Deepak



 
Greenhorn
Posts: 1
Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
line 8 in testParameters is missing

Here: ExecParameters[] execParameters = new ExecParameters[3]; you're only allocating memory for array and array of ExecParameters is filled with null objects. I don't know how you get into line 20 ;)
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember null pointer exceptions are always due to no object creation.
 
lowercase baba
Posts: 13050
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are getting an error, please post it, in its entirety here. Those error messages tell you EXACTLY where the problem is. Your job when posting here is to make it as easy as possible for us to help you, and posting that message is a basic requirement.
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad prap wrote:Remember null pointer exceptions are always due to no object creation.



"Always" is a little strong, Prasad. null can be assigned as well:



Output:

And, one should not be confused into thinking that "null" is somehow an illegal value for an object variable to have in it, or even pass to another object's methods. For example:



Gets you this:


You know, I would love to get my hands on the person who decided to name that one NullPointerException. C/C++ programmers who learn Java all have to come to terms with the words, "there are no pointers in Java." (Well, I had to, anyway.) Object variables with null in them are part of what a Java programmer has to understand. Tossing error messages with the word "pointer" embedded in them at a new Java coder just adds to the confusion.
 
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic