how to check if ia m having null values..

Posted on 2008-10-10
Medium Priority
Last Modified: 2013-11-23
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
Question by:cutie_smily

Expert Comment

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

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

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.

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.

Expert Comment

ID: 22685628
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


Expert Comment

ID: 22687365

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.

   {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.

Accepted Solution

mkopka earned 750 total points
ID: 23146150
Also, you say that you want to check if its Null and throw a NullPointerException or RuntimeException if it is null.  This isn't the way I would go about this. The answers above show you how to check for a null or if its empty value.

I would do this check as shown above but rather than throw an exception indicating this has occurred I would preferably deal with it in whatever way you would've when catching the exception.

ie.e.: rather than:

//Note: String doesnt have a isEmpty() method, rather you need to check for Null and blank instead.
if (!userPhone.isEmpty()) {    
  throw new NullPointerException("empty value in userPhone");

and then later catching that exception and trying to deal with it i would prefer:

//i.e.: check userPhone isn't null, empty, or made up entirely of white space (hence the trim())
if (userPhone == null || userPhone.trim().equals("")) {
} else {

and the doSomething() call I would replace with functionality that deals withthe case of an empty / not-provided phone number. i.e.: Do you default to a default phone number in this case, do you provide an empty "" phone number, do you not set that value in this case, etc. IMHO its better to deal with this sort of simple logic here rather than throwing an exception, having to catch it later and then later deal with it. Its always best to deal with unexpected things as close to the root of the cause as possible.

Especially in the case of something as basic as a value not being provided, i would just add the code to deal with that. Exceptions should be kept for 'exceptional' circumstances rather than being used as a control flow (i.e.: replacement for if/else or goto (not in java ofcourse), or other control flow commands).  

There are a number of reasons behind this statement, including Exceptions causing more of a performance hit than simply adding code to deal with the situation of a blank / un-provided value (since exceptions need to find their place in the stack, generate the stacktrace, fill out the exception class, and then interrupt execution to throw the exception). However, this performance hit in most cases isnt the main reason. The main reason IMHO is the fact that throwing an exception simply adds a layer of complexity that's not needed and that could be easily replaced by a simple block of code indicating what to do in the highly likely case of a value not being found and coming back as null (in this example).

Author Comment

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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month9 days, 12 hours left to enroll

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question