| Author |
strange NullPointerException
|
John Eric Hamacher
Ranch Hand
Joined: Apr 25, 2007
Posts: 230
|
|
Hello: I am getting a NullPOinterException at this line: previousState[i] is a java.util.List of size 0; Anybody know why this is happening? Thanks.
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
It looks like previousState is null, so previousState[i] can't reference anything. If you don't believe that's the case, try printing out the value just before this line:
|
"I'm not back." - Bill Harding, Twister
|
 |
Stevi Deter
Ranch Hand
Joined: Mar 22, 2008
Posts: 265
|
|
If List is of size 0, it has no elements, so you can't actually access a specific element. You can add a null to a List, but you can't access an element that doesn't exist.
|
There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
|
 |
John Eric Hamacher
Ranch Hand
Joined: Apr 25, 2007
Posts: 230
|
|
|
Thanks. I'm not accessing a member of the List itself, previousState[i] is a List. previousState is just an array of Lists.
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
Well, previousState is supposed to be an array of lists. However I think it's actually null, and that's why you're getting the exception. If you're certain that the NPE stack trace points to that line, then this is the only possibility. I suppose we must also consider the possibility that, for example, you've got the wrong line, perhaps because the .java file hasn't been freshly compiled into a .class file (you've got an old class file lying around). If that's the case, it will become obvious when you att the System.out.println() statement I suggested.
|
 |
Stevi Deter
Ranch Hand
Joined: Mar 22, 2008
Posts: 265
|
|
Then I'll defer to Jim's accurate answer. If previousState is null, you'll get an NullPointerException trying to access an element of the array. If previousState doesn't contain i+1 elements, previousState[i] will return an IndexOutOfBoundsException. Only if previousState is not null, and contains i+1 elements, will previousState[i] be able to return the value at index i. Unless, of course, the assumption that i is an int is wrong and you're using an Integer. In which case, if Integer i is null, that could also throw an NPE. [ May 07, 2008: Message edited by: Stevi Deter ]
|
 |
John Eric Hamacher
Ranch Hand
Joined: Apr 25, 2007
Posts: 230
|
|
|
You're both right. previousState was null. I guess that surprised me. I had just taken the steps to prevent this from happening in HIbernate so I wasn't expecting it to actually happen! Its the end of a long day.
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
[Stevi]: Unless, of course, the assumption that i is an int is wrong and you're using an Integer. In which case, if Integer i is null, that could also throw an NPE. Oh, good point, I forgot that possibility. Glad to hear it worked out, John.
|
 |
 |
|
|
subject: strange NullPointerException
|
|
|