Link to home
Start Free TrialLog in
Avatar of cutie_smily
cutie_smily

asked on

how to check if ia m having null values..

I am getting recvords from database values in XML Object ..so i retriving using XPath as shown below
String userPhone=new String( this.xpathFactory.newXPath().evaluate( "/row/UADD_PHNE", document ) );

How do i check above value is null then i need to throw an exception (Runtime exeception where if null needs to skip that record) i do not want the null string into my flat file


What is try{ }    catch( Throwable e ) { //How is Throwable different from Exception e

or NullpointerException e
Avatar of Monky42
Monky42

Several questions at once =)
How the check for null value:

String userPhone = this.xpathFactory.newXPath().evaluate( "/row/UADD_PHNE", document );
if (userPhone == null) {
 doSomething();
} else {
doSomethingElse();
}


How to throw an exception:
throw new RuntimeException();

What is try-catch:
You can use this construct to catch an exception. This means you can define what should happen, if an exception of type X occurs. This can be useful to recover from errors without ending your process. For more detail on try-catch please consult a java handbook or try google.

Throwable:
The Throwable class is the superclass of all errors and exceptions in the Java language. Instances of two subclasses, java.lang.Error and java.lang.Exception, are conventionally used to indicate that exceptional situations have occurred.

Hope this helps.
You could only check if the userPhone value is empty.

you should catch an Exception instead of Throwable. because when you catch throwable it is possible that you handle all unexpected errors.
go to this url for a short description: http://wiki.answers.com/Q/What_is_the_difference_between_catch_exception_e_catch_error_err_and_catch_throwable_t

Try this url for further information on exceptions: http://java.sun.com/docs/books/tutorial/essential/exceptions/
if (!userPhone.isEmpty()) {
  throw new NullPointerException("empty value in userPhone");
}

Open in new window


Throwable is the superclass of Exception and Error.
Error is usually not handled.Exceptions are of two types.Unchecked and checked exceptions.
Unchecked exceptions are not usually handled unlike the checked exceptions.
So for a good design you code as below.

if(!userPhone.isEmpty())  
   {note the not symbol, and perform the required operation}
but if you require to handle it then
throw new NullPointer Exception() in the condition.

We usually dont catch or throw general exceptions like RunTimeException, the exceptions need to be more specific.
ASKER CERTIFIED SOLUTION
Avatar of mkopka
mkopka
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cutie_smily

ASKER

sorry! thought of splitting point between answers. I could not get how to do that..is there a way to get back do