• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to avoid NoPointerException  RSS feed

 
Jing Liang
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I need some help on this one.
So if I created an array, lets say Object test[] = new Object[10]
so test.length is 10. But I am going to fill the test array to 5.
When I use a loop(i<test.length) to look up the test[], I will encounter NPE at test[6,7,8..]. How do I avoid this NPE? Thanks
 
Jing Liang
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry guys I mean NullPoiterException. Thanks
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check if the content is null:

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No magic here... if there is a chance that certain members of the array are null, then you just have to only work on those that are not null.



Henry
 
Jing Liang
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Satou and Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!