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

Trying to access an object's methods

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
very likely the users[numOfUsers] is returning null, so it failed when null is used to call getName()
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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



 
Yeah, but is it art? What do you think tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic