• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to access an object's methods  RSS feed

 
Jacob Steingart
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an array of objects, and I'm trying to access a getter with code like this:



By the way, showName is a static variable, not a method or undeclared variable. Right, so when I run this code, I get a NullPointerException in runtime. Does anyone know what I'm doing wrong?
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
very likely the users[numOfUsers] is returning null, so it failed when null is used to call getName()
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I get a NullPointerException in runtime.

generally indicates this type of structure

Person[] users = new Person[10];
showName = users[numOfUsers].getName();//NPE here

missing is this bit (on its own, or in a loop)
users[x] = new Person();

the first bit declares the space [10]
the missing bit creates each object
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most likely the snippet:



is returning a null value. So when you try to call


you calling getName on a null object, and thus getting a NullPointerException.

Can you show the code where you populate the array?

Remember that object arrays default to null objects. Only arrays of primitives (ints, bytes, booleans, floats, etc) are initialized with default values.

So if I have the code:



I get an array of 10 null objects.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LOL - looks like you had three of us all typing up an answer at the same time. But since we all basically said the same thing... we must be on the right track
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
may be your array is just declared but not initialized ! , You can always print array element or check for its nullity before invoking any method !
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
me too Mark, just 2 minutes late in replying !
 
Jacob Steingart
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure I declared it... here is the code I'm using. I have a lot of commented out code, so I hope it isn't too messy .



It's probably something really tiny and stupid that I'm missing, but I have a tendency too miss these things. Thanks in advance!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your problem might be in this bit:

users[numOfUsers] = new User(tempUName,tempPass);
numOfUsers++;//<------------------
saveUser();

in this, you create the User(), then increment numOfUsers.

try it like this, and see if it still errors
showName = users[numOfUsers-1].getName();

(also check showName gets the correct name)
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although Michael has isolated the problem,
showName = users[numOfUsers-1].getName();

this code can throw the ArrayIndexOutOfBoundsException if numOfUsers is zero.
The same thing can happen if numOfUsers is greater than 50.

So I think a better option would be to slightly modify the code so that numOfUsers properly points to the required location.
And also make sure that numOfUsers is always less than the maximum limit, 50 in this case.


Hope this helps
 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Unrelated to the question, but, aren't you forgetting to do something with result here?



 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!