Tuesday, March 8, 2011

Check Null Pointer Exception in Java with example code

Here is the information and sample example program for avoiding Null Pointer Exception in Java. The first thing is that you need to check the specified variable null or not. In Basic Null Pointer Execptions are the unchecked exception thrown by java compiler. When the program attempt to use a particular variable for certain functionallity the variable should not be a null value. In Java development Environment this is the main thing need to check always by using some condition. Otherwise some cases when the user is accessing the developed appication it will throw the null pointer exception which user can't able to understand this kinds of error. So it is required to check in development face itself. We need to give a valid general information to the user or we need to redirect to some general page when this is kinds of expcetion occur.

How to aviod null pointer exception in Java Program?

You can avoid this major null pointer exception by following some simple code in your program. To aviod null pointer exception in your program you need to always check the variable is null or not null.

The code given below is used to check null pointer exception in Java program

if(varName!=null){
varNameValue=varName;
}else{
varNameValue=defaultValue;
}

This is simple to follow always check variable is not null or null. This will help the program to prevent null pointer exception in your program.

 
Design by Free Wordpress Themes | Bloggerized by Free Blogger Templates | Web Hosting Deals