• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

multidimensional array

 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int a[][]=new int[5][];

does this mean that
a[0]=null;
a[1]=null; and so on till a[4] ? i think this is why i get null pointer exception when i try to sue a[0][0];

sorry for asking so many questions...just making sure all my concepts are clear



why doesnt this me arrayoutofindexexception?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raju Champaklal wrote:int a[][]=new int[5][];


As far as the code is concerned it does throw an ArrayIndexOutOfBoundsException but as you placed it inside the initializer the exception is thrown when an object of this class is created/instantiated. Try writing new A() in main() method to check it out.

Going back to your first question:

Raju Champaklal wrote:
int a[][]=new int[5][];

does this mean that
a[0]=null;
a[1]=null; and so on till a[4] ? i think this is why i get null pointer exception when i try to sue a[0][0];



This is an array of arrays so the cells you refer to by the first dimension are actually references to the second array so they indeed are initialized to contain null values. This also implies that you cannot refer to a[0][0] since there isn't anything initialized there which results in NullPointerException.

Regards,
Paul.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh yes...i got that error...i was actually expecting to get Exceptionininitializererror...when this exception thrown?
 
Paul Prusko
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raju Champaklal wrote:oh yes...i got that error...i was actually expecting to get Exceptionininitializererror...when this exception thrown?



ExceptionInInitializerError is thrown when something goes wrong within static initialization block while your exception was thrown in instance initializer. You can obtain it by simply reimplementing your class as follows:



Regards,
Paul.
 
reply
    Bookmark Topic Watch Topic
  • New Topic