• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

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: 13089
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.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic