Solved

how to check if ia m having null values..

Posted on 2008-10-10
5
238 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…

760 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

18 Experts available now in Live!

Get 1:1 Help Now