Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException using null in Array

 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At
http://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html
Oracle lists the following reasons that NullPointerException occurs.

Thrown when an application attempts to use null in a case where an object is required.
These include:
1. Calling the instance method of a null object.
2. Accessing or modifying the field of a null object.
3. Taking the length of null as if it were an array.
4. Accessing or modifying the slots of null as if it were an array.
5. Throwing null as if it were a Throwable value.
-----
In the code sample below, there is a difference between

a) line 1: Assigning null to an array, this will throw NullPointerException
b) line 2: Having a null element in the second array - null will be printed.



For the purpose of clarification:

Question:
Is reason number 4 listed above at
http://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html

"4. Accessing or modifying the slots of null as if it were an array"

demonstrated by line 1 of the code?

Thanks
()()()
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Hauck wrote:demonstrated by line 1 of the code?

Yes. The 2nd element of your outer array is null, and you're trying to access an index of it, as it was an array in your for loop. This happens on the 2nd iteration of outer loop, when you are accessing 2nd element of outer array.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Hauck wrote:Question:
Is reason number 4 "4. Accessing or modifying the slots of null as if it were an array" demonstrated by line 1 of the code?

Yes, it is. Good example!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic