• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting Null pointer while setting array of objects  RSS feed

 
Deepak harsha
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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



 
Michal Czubek
Greenhorn
Posts: 1
Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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 ;)
 
Prasad prap
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember null pointer exceptions are always due to no object creation.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!