• Post Reply Bookmark Topic Watch Topic
  • New Topic

array  RSS feed

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


It throws exception in this line:
java.lang.NullPointerException

Why?
 
Shashank Gollapudi
Ranch Hand
Posts: 33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


Here in this case, after the execution of line 3 you have a reference to an array of size three whose contents are null, as you haven't mentioned the second dimension in the array definition.

nums2[0] points to first element in the array which is null and nums2[0][0] gives you a null pointer exception.

Instead you can alternatively mention the second dimension or give nums[0] = new int[4]; before your line 4.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nums2[0] points to first element in the array which is null and nums2[0][0] gives you a null pointer exception.


I cann't understand.still don't know why it throws exception.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A two-dimensional array in Java is an array, whose each element's type is of the one-dimensional array. The very elements can be null (for example if not initialized like in our case) or arrays of different sizes.

$ java a
null
null
null



 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
$ java a
null
null
null
[I@923e30
[I@130c19b
null

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!