, , , , NPE, , OP .
, () Object, , null.
SomeObject someObject1 = new SomeObject();
SomeObject someObject2 = null;
, (null), , , NullPointerException, , null .
someObject1.doSomething();
someObject2.doSomething();
. : , .
if (someObject2 == null) {
someObject2 = new SomeObject();
}
someObject2.doSomething();
if (someObject2 != null) {
someObject2.doSomething();
}
NPE stacktrace , . " 272 admin.birthList.add(list1);". , / ( dot .). admin.birthList, - birthList.add(list1). , , NPE. , admin null. , birthList null. , .
: ( ), . " " System.out.println() , / . , NPE. , ,
admin.birthList.add(list1);
, :
System.out.println("admin: " + admin);
List<Birth> birthList = admin.birthList;
System.out.println("birthList: " + birthList);
birthList.add(list1);
, - null. :
if (admin == null) throw new NullPointerException("admin is null!");
List<Birth> birthList = admin.birthList;
if (birthList == null) throw new NullPointerException("birthList is null!");
birthList.add(list1);
, , , null.
List<Birth> birthList = admin.birthList; // If NPE line points here, then admin is null.
birthList.add(list1); // If NPE line points here, then birthList is null.