• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Arrays doubt - pls clarify

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

Which of the commented lines will generate an exception at runtime?

class Z extends Object {

static int godSmack[];
int danzig[];

static void main(String args[]) {
System.out.println(godSmack); //line 1
System.out.println(godSmack[0]); //line 2
System.out.println(new Z().danzig); //line 3
}
}

Answer is 'line 2'.

Can someone pls explain why line 2 will generate an exception and why line 1 and lline 3 will not?

Thanks in advance!
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 1 and line 3 are both Instance variables so they have the value of "null".
Line 2 you are trying to access an array element which doesnt exist so you got NullPointerException
[ May 22, 2007: Message edited by: Ahmed Yehia ]
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
-----------------------------------------------
static int godSmack[];
int danzig[];
--------------------------------------------
The above these statements are by default
static int godSmack[]=null;
int digzig[]=null;

if you print
System.out.println(godSmark)//prints null
because it contains null

System.out.println(godSmark[0]);//Exception

Because it is not pointing to any array in the heap,but you are trying to access the 0th element.

That's you are getting Exception.

Thanks

Anil Kumar
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By default, when you print an array it will give its hashcode value. It holds good for any object until the toString() method is not overridden.

Since an array is an object and its not pointing to any object (rather, its pointing to nowhere - NULL), you will get *null* in the console (if through SOPs).

In case of second statement, you are trying to access the 0th element of an array through its reference variable which is currently pointing to a non-existing object. So you get an EXCEPTION

HtH.
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Line 2 you are trying to access an array element which doesnt exist so you got NullPointerException



Hi Ahmed,
It causes ArrayIndexOutOfBounds Exception not NullPointerException


Regards
Nikhil
 
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


posted Today 12:13 PM Profile for nik arora Edit/Delete Post Reply With Quote

quote: Line 2 you are trying to access an array element which doesnt exist so you got NullPointerException



Hi Ahmed,
It causes ArrayIndexOutOfBounds Exception not NullPointerException


Regards
Nikhil



Hi Nikhil,

It won't give ArrayIndexOutOfBounds because array object is null, it gives NullPointerException


Regards,

Abdul Mohsin
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. NullPointerExcpetion is for any object which is being accessed but does not exist in the Heap.

Here you are trying to access the array object through its reference but it does not have a valid entry in Heap. Means, its not pointing to any object in heap.

godSmack[0] is something like godSmack.get(0) only. Thats where you get the NullPointerExcpetion and NOT ArrayIndexOutOfBoundsException.

NullPointerException has a higher precedence than ArrayIndexOutOfBounds while dealing with Arrays.

HtH.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic