Saturday, February 14, 2009

Java Issues and Solutions - 3

Common Exceptions and Solutions:

java.lang.NullPointerException;
Problem:The JVM throws null pointer exception when object accessing is dereferenced or not assigned any value.
Ex:
String s;
s.toUpper();
Customer c = null;
c.getName();

Solution:
Make sure that the object is assigned a proper value or its not dereferenced.
Ex: String s = "Test";
s.toUpper();
StringBuffer s = "";
s.append("test");
Customer c = new Customer();
c.getName();

Best Practise: Add null check before accessing any object.
Ex:
Customer c = someObject.getCustomer();
if( c != null)
{
c.getName();
}

java.lang.ClassCastException;
Problem:
The JVM throws class cast exceptions when your are trying to type cast one object which is not a type or sub type of that object.
Ex: X x = (X) y ; here Y should be either instance of X or it should be subtype of X.
class Customer{ -- }
clas Vehicle{ -- }
Vehicle v = new Vehicle();
Customer c = (Customer)v;
Here v is not an instance of Customer and its not s subtype of Vehicle, if you are trying to do this casting the JVM cannot caste the Vehicle object to Customer then it throws class caste exception.

Solutions:
Make sure that your trying to cast the appropriate type that is the exact instance of that class or sub type of that.
Ex:
X y = new X();
X x = (X) y;
class Vehicle{--}
class Car extends Vehicle{--}
Car c = new Car();
Vehicle v =(Vehicle)c;

BestPractise: Use intanceof check before casting the object
Ex:
class Vehicle{--}
class Car extends Vehicle{--}
Car c = new Car();
if( c instanceof Vehicle)
{
Vehicle v =(Vehicle)c;
}

java.lang.ArithmeticException:
Problem:
Mostly this exception will be thrown while trying to divide one variable or value by zero.
Ex:
int x = 10;
int result = x/0; or
int y = 0;
int result = x/y;

Solution: Make sure the divider value is not zero
BestPractise: Use != 0 check or >0 check for the divider before using in the arithmetic operation depending on the scenario.
int x = 10; int y = 0;
if(y > 0)
{
int result = x/y;
}

java.lang.NumberFormatException:
Problem:
This exceptions is thrown when your are trying convert a non numeric string to numeric.
Ex:
String responseCodeString = "x123";
int responseCode = Integer.parseInt(responseCodeString);
Solution:
Make sure that the string contains only digits or keep the statement in try catch block String responseCodeString = "123";
try{
int responseCode = Integer.parseInt(responseCodeString);
}catch(Exception e)
{
}

BestPractise: Use try catch block for this type of statements

java.lang.ArrayIndexOutOfBoundsException:
Problem:
This exception will be thrown while trying to access an index in an array which is more than the length of the array.
Ex:
String[] books = new String[3]; //allocates 3 places with index of 0,1 and 2
String thirdBook = books[3]; //the index 3 is not available in the above created array, it throws exception
Solution:
Make sure that you are using proper index before accessing the array or use the .length property to get the array size and do the operation.
String[] books = new String[3];
String thirdBook = books[2];// the index 2 is available in the above created array which returns third element
BestPractise:
Use .length property of the array before accessing its elements. int index = 3;
int size = books.length;
if(size > index)
{
String bookName = books[index];
}
This is mainly used in for loops as below.
for(int i=0;i<books.length;i++)
{
String bookName = books[i];
}

java.lang.NoClassDefFoundError:
Problem:
This exception is thrown when the specified class is not available at run time, this may happen because the class is not present in the current directory and not in the class path.
Solution;
1.Make sure that class is available in the current directory or
2.Make sure that class path is set for the specified class if it is not in the current directory or
3.Make sure that jar file is added in the class path if the class is present in any jar file
BestPractise:
Place all the classes or jar files at one place(like lib folder) and set the path to the place.

java.lang.IllegalStateException:
Problem:
This exceptions thrown when you are performing illegal operations on the object like accessing a closed IO stream or starting a thread which is already started etc.,
Ex:
1. con.close();
con.flush(); //The connection is already closed there is no meaning to flush the data for the connection.
2. t1.start(); //where t1 is a thread
t1.start(); //The thread has been already started again calling start leads to illegal state exception
Solution:
Check for the scenarios like where trying to performing illegal operation

java.lang.NoSuchMethodException:
Problem:
When we are calling the method on the object is not available
Solution:
Check for the below scenarios.
1. Check whether the calling method is defined on that object
2. Check whether the access modifier is private if it is accessing from the other class
3. Check whether the parameters and return types are proper

No comments:

Post a Comment