• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception Handling  RSS feed

 
Ravi Dagar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I put the highlighted text in try/catch block it is throwing NullPointerException , if I am using command line arguments then also it is showing the same exception.
Please explain!!

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you catching in your catch block and why do you think that it should not throw a NullPointerException?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It really should be no surprise that if you de-reference something that you just set to null, that it will result in a null pointer exception.
 
Ravi Dagar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it so that if we set something to refer to null we cannot refrence it to something other?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, you can set the variable to refer to something else, but you haven't.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
of course you can. however "args" is not the same thing as "args[0]".

your 'args' is a box with a number of slots in it (a String array). You then throw the box away with the "args = null".

So when you then try and put something in the first slot of the box, Java is telling you "ummm...you don't have a box any more, so you can't put something in that box's first slot".
 
Ravi Dagar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay.Now I am getting it.I actually set the complete refrence variable to null and then assigning its parts some value.
Right?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Why do you feel the need to set it to null in the first place?
 
Ravi Dagar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I was doing some questions and the code was designed something like this only.
So I just implemented that code only.
 
Ravi Dagar
Greenhorn
Posts: 24
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fred.
I got to understand your explanation.I actually set the complete args to nulland then I am setting args[0] to some value which obviously throw exception because args[0] does not even exist.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi

I would investigate the difference between

and

Remember, both arrays and Strings are objects.
 
Ravi Dagar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you James.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!