NullPointerException是运行时异常,当应用程序尝试使用具有空值的对象引用时,将引发NullPointerException。
例如,对空引用使用方法。
public class Tester {
public static void main(String[] args) {
Object ref = null;
ref.toString(); // this will throw a NullPointerException
}
}输出结果
Exception in thread "main" java.lang.NullPointerException at Tester.main(Tester.java:4)