Forums Register Login

Exception Handling

+Pie Number of slices to send: Send
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!!

+Pie Number of slices to send: Send
What are you catching in your catch block and why do you think that it should not throw a NullPointerException?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Is it so that if we set something to refer to null we cannot refrence it to something other?
+Pie Number of slices to send: Send
Sure, you can set the variable to refer to something else, but you haven't.
1
+Pie Number of slices to send: Send
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".
+Pie Number of slices to send: Send
Okay.Now I am getting it.I actually set the complete refrence variable to null and then assigning its parts some value.
Right?
+Pie Number of slices to send: Send
No. Why do you feel the need to set it to null in the first place?
+Pie Number of slices to send: Send
Actually I was doing some questions and the code was designed something like this only.
So I just implemented that code only.
1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Ravi

I would investigate the difference between

and

Remember, both arrays and Strings are objects.
+Pie Number of slices to send: Send
Thank you James.
Get me the mayor's office! I need to tell him about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 574 times.
Similar Threads
Array : Multi-dimensional
Testing args[0]
How Java Compiler check the code !!!
Packed decimal conversion - Java
For the Love of Loops!
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 05:05:46.