Improve company productivity with a Business Account.Sign Up


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

Try this url for further information on 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 there a way to get back do

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This video teaches viewers about errors in exception handling.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

595 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