| Author |
please tell me what will be the output of this and why?
|
gunjan khanuja
Ranch Hand
Joined: Apr 16, 2012
Posts: 37
|
|
[Added code tags - see UseCodeTags for details]
|
 |
Praveen Kumar M K
Ranch Hand
Joined: Jul 03, 2011
Posts: 256
|
|
Hi Gunjan,
Did you run this program? If yes, what was the output that you got?
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3793
|
|
Hi Gunjan. Welcome to the Ranch!
Praveen's right. The best way to find out what the output is is to run it yourself. Then, if you can't work out why that is the output, come back and we'll help you out.
|
 |
gunjan khanuja
Ranch Hand
Joined: Apr 16, 2012
Posts: 37
|
|
yes i tried and i got null
pointer exception ..but why did i get this output ..could you please explain..?
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3793
|
|
|
Well, on line 6 you're trying to access an element of a null array. What would you expect to happen?
|
 |
gunjan khanuja
Ranch Hand
Joined: Apr 16, 2012
Posts: 37
|
|
|
no but then i am also giving zeroth element a value in another line...
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56191
|
|
|
You can't add anything to a null array.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
gunjan khanuja
Ranch Hand
Joined: Apr 16, 2012
Posts: 37
|
|
|
Thanks i get it...
|
 |
Praveen Kumar M K
Ranch Hand
Joined: Jul 03, 2011
Posts: 256
|
|
The array variable arr is pointing to null in line 5 and in the next line you are trying to "dereference" the null pointer. It is this line which is causing the exception.
In case you have not yet gone through what a null pointer is and what a NullPointerException is , do search this forum, there are plenty of posts on the same.
Thanks,
Praveen.
|
 |
 |
|
|
subject: please tell me what will be the output of this and why?
|
|
|