Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Trying to access an object's methods Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Trying to access an object Watch "Trying to access an object New topic
Author

Trying to access an object's methods

Jacob Steingart
Ranch Hand

Joined: Mar 28, 2008
Posts: 63
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?


I never know what to put in my signature...
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 195
very likely the users[numOfUsers] is returning null, so it failed when null is used to call getName()
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Dec 17, 2003
Posts: 624

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

Joined: Dec 17, 2003
Posts: 624

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

Joined: Feb 19, 2008
Posts: 2896
    
    1

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 !


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2896
    
    1

me too Mark, just 2 minutes late in replying !
Jacob Steingart
Ranch Hand

Joined: Mar 28, 2008
Posts: 63
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

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Jun 06, 2007
Posts: 2552
    
    2

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


SCJP, SCWCD.
|Asking Good Questions|
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117


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





Java hobbyist.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Trying to access an object's methods
 
Similar Threads
Marcus mock exam 2 (quest. #32)
Heather MacKenzie's note
access to static intilization variables
Accessing local environment variable
Question for variable initialization order