import org.apache.commons.lang3.StringUtils;
public class NullPointerExceptionExample {
private static void printLength(String str) {
if (StringUtils.isNotEmpty(str)) {
System.out.println(str.length());
} else {
System.out.println("Empty string");
}
}
public static void main(String args[]) {
String myString = null;
printLength(myString);
}
}
public class Cat{
private Integer id;
private String name;
public void setId(Integer id){
this.id=id;
}
public Integer getId(){
return this.id;
}
}
public static void main(String[]args){
Cat cat;//nullpointerexception
// you should change Cat c=new Cat();
cat.getId();
}
CartPage cartPage = null;
@Before
public void initTest(){
cartPage = PageFactory.initElements(driver, CartPage.class);
}
int x;
int y = x + x;
Integer num;
num = new Integer(10);
Integer num;
num = new Integer(10);
TestObject a = new TestObject();String b = a.getTestResult();b.toString();
public class MyClass{
private Object iAmNull;//This is null by default
private Object iAmInitialized=new Object();
public void test(){
System.out.println(iAmInitialized.toString());//prints that object
System.out.println(iAmNull.toString());//throws a NullPointerException
}
}