File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please tell me what will be the output of this and why?

 
gunjan khanuja
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


[Added code tags - see UseCodeTags for details]
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gunjan,

Did you run this program? If yes, what was the output that you got?
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i tried and i got null
pointer exception ..but why did i get this output ..could you please explain..?
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no but then i am also giving zeroth element a value in another line...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't add anything to a null array.
 
gunjan khanuja
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks i get it...
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic