This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

strange NullPointerException

 
John Eric Hamacher
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Eric Hamacher
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic