Solved

how to check if ia m having null values..

Posted on 2008-10-10
5
239 Views
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
0
Comment
Question by:cutie_smily
5 Comments
 
LVL 2

Expert Comment

by:Monky42
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) {
 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.
0
 
LVL 6

Expert Comment

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

0
 
LVL 2

Expert Comment

by:formula1act
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.

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.
0
 
LVL 2

Accepted Solution

by:
mkopka earned 250 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("")) {
   doSomething();
} else {
  doSomethingElse();
}

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).
0
 

Author Comment

by:cutie_smily
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now