NullPointerException(常缩写为 NPE)指在(尤其是 Java 等语言中)程序试图对一个为 null 的对象引用进行访问或调用(如访问字段、调用方法、取数组长度等)时抛出的运行时异常。常见原因是对象未初始化、返回值为空却未做判空处理等。
/ˌnʌl ˈpɔɪntər ɪkˌsɛpʃən/
I got a NullPointerException because the object was null.
我遇到了 NullPointerException,因为那个对象是 null。
The service threw a NullPointerException when the database query returned null and the code tried to call .trim() on it without a null check.
当数据库查询返回 null,而代码在未做判空的情况下对其调用 .trim() 时,服务抛出了 NullPointerException。
该词由三部分组合而成:null(“空值/无”)+ pointer(“指针/引用”)+ exception(“异常”)。最早在面向对象语言的语境中广泛使用,尤其与 Java 的异常命名习惯(以 XxxException 结尾)高度契合,用来明确描述“空引用导致的异常”。