Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array : Multi-dimensional

 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This compiles and runs



This compiles but throws NullPointer exception



Can someone please explain this ?
 
Henry Wong
author
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first case, you created an array of int arrays -- and created the int array for each element.

In the second case, you just created an array of int arrays -- but did not create the int array for each element.

Henry
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can you say that "did not create the int array for each element" ? Did not understand.
 
Henry Wong
author
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arnb Sen:
How can you say that "did not create the int array for each element" ? Did not understand.


That is how it works... when you say...



Java will create you an array of int arrays but will not create each element. To do that, you must...



And also notice that each array element does not have to be an int array of the same size.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic