?
Solved

how to check if ia m having null values..

Posted on 2008-10-10
5
Medium Priority
?
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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("")) {
   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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

770 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