aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes please tell me what will be the output of this and why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "please tell me what will be the output of this and why?" Watch "please tell me what will be the output of this and why?" New topic
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: 4392
    
    8

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: 4392
    
    8

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: 61241
    
  66

You can't add anything to a null array.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by 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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please tell me what will be the output of this and why?