Sunday, 12 July 2015

How to compare two unknown string objects without null pointer exception ?

Ex :
String one = request.getAttribute("value1");
String two = request.getAttribute("value2");

Possible to get null pointer exception :

if(one.equals(two)) {
    System.out.println("possible to through null pointer exception");
}

To avoid null pointer exception :

if(one != null && one.equals(two)) {
    System.out.println("we can not get null pointer exception here");
}

Note : Here, if condition one true then only it will check condition two.

No comments:

Post a Comment