Can anyone help me how to handle JAVA NullPointerException?
Is it possible to continue to run in "<>" as shown below?
Or I need to assign a new value if String A is null?
Please advice. Thanks.
String AA = getXMLInfo(XXX, "YY");
String A = myDate.convert(AA, inDateFormat, outDateFormat);
String BB = getXMLInfo(VVV, "UU");
String B = myDate.convert(BB, inDateFormat, outDateFormat);
catch (NullPointerException e)
{
System.out.println("main: Oops, null exception caught");
<continue to run and assign
A=B, C=D;>
}
Roedy Green - 19 Jan 2010 12:33 GMT
>Can anyone help me how to handle JAVA NullPointerException?
>Is it possible to continue to run in "<>" as shown below?
There is nothing about XML in your question. Did you forget to post
part of your question?

Signature
Roedy Green Canadian Mind Products
http://mindprod.com
Responsible Development is the style of development I aspire to now. It can be summarized by answering the question, How would I develop if it were my money? Im amazed how many theoretical arguments evaporate when faced with this question.
~ Kent Beck (born: 1961 age: 49) , evangelist for extreme programming .
Lew - 19 Jan 2010 14:00 GMT
> Can anyone help me how to handle JAVA NullPointerException?
> Is it possible to continue to run in "<>" as shown below?
Yes.
> Or I need to assign a new value if String A is null?
That depends on the logic of the problem domain.
> Please advice. Thanks.
>
[quoted text clipped - 12 lines]
> A=B, C=D;>
> }
Normally you don't have a lot of code in the 'catch' block; that's the
exceptional code path and most logic should be in the unexceptional path. In
the 'catch' block you do just enough to restore sane program state - perhaps
you assign a default value to 'A' [1] or return prematurely from the method.
Whatever you do, you should log the exception in the 'catch'. Normal program
logic will resume outside the 'catch' with sanity restored.
'NullPointerException' ("NPE" for short) is a programmer error. Normally you
wouldn't have a 'catch' block for runtime exceptions but would fix the code so
they cannot happen. Fix your code so that the reference cannot be 'null'.
[1] You should follow the coding conventions, which dictate that only type
names and constants begin with an upper-case letter. Variable names (that
aren't constants) and method names should begin with a lower-case letter.
Non-constant identifiers should be in camel case - mixed case with the start
of each compound word part capitalized, as in 'SomeType' or 'aVariable'.
Avoid one-letter or similarly short, obscure variable names - what the hell is
an "AA" supposed to be? How does that name even remotely help a maintainer
understand what is going on? Spell it out with a domain-meaningful word or
phrase. C'mon - bits aren't expensive.
String documentInfo = getXMLInfo( document, "YY" );

Signature
Lew
Roedy Green - 20 Jan 2010 12:34 GMT
>Can anyone help me how to handle JAVA NullPointerException?
>Is it possible to continue to run in "<>" as shown below?
[quoted text clipped - 16 lines]
> A=B, C=D;>
> }
Your code gives Java programmers indigestion because you are ignoring
the naming conventions A and BB are normally class names, not variable
names. a and bb are variable names. See
http://mindprod.com/jgloss/codingconventions.html
you need
try { ... } catch ( NullPointerException e ) { recovery... }
Triggering an exception is a quite slow operation. So normally you
would handle it like this with explicit checks:
String a = (value != null) ? value : "huh?";

Signature
Roedy Green Canadian Mind Products
http://mindprod.com
Responsible Development is the style of development I aspire to now. It can be summarized by answering the question, How would I develop if it were my money? Im amazed how many theoretical arguments evaporate when faced with this question.
~ Kent Beck (born: 1961 age: 49) , evangelist for extreme programming .