• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array Reference  RSS feed

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Please explain me why this code is giving run time error.




 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A 2-dimensional array in Java is really an array of 1-d arrays. When you initialize using new int[3][], you get an array of three pointers to int arrays; all of those pointers are null. In your example, you assign an int[] for number[0] to point to, but scores[0] is still null, hence the error when you try to access an element of the (nonexistent) array it points to.
 
Mubeen Shaik
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest,

Thanks for the reply. I was confused with the arrays concept. Now it is clear.

I did the following :

scores[0] = new int[3];

// The above line of code will create three elements in scores[0]? --
// Is my java terminlogy is correct ? - Please suggest.


scores[0][1] = number[0][0]; // Correct ?

Thanks,
Mubeen.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will now work, and your terminology is correct.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!