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
posted
0
very likely the users[numOfUsers] is returning null, so it failed when null is used to call getName()
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.
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
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.