Java 什么是 NullPointerException,我该如何解决?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/218384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a NullPointerException, and how do I fix it?
提问by Ziggy
What are Null Pointer Exceptions (java.lang.NullPointerException
) and what causes them?
什么是空指针异常 ( java.lang.NullPointerException
) 以及导致它们的原因?
What methods/tools can be used to determine the cause so that you stop the exception from causing the program to terminate prematurely?
可以使用哪些方法/工具来确定原因,以便阻止异常导致程序过早终止?
采纳答案by Vincent Ramdhanie
When you declare a reference variable (i.e. an object) you are really creating a pointer to an object. Consider the following code where you declare a variable of primitive type int
:
当您声明一个引用变量(即一个对象)时,您实际上是在创建一个指向对象的指针。考虑以下代码,您在其中声明了一个原始类型的变量int
:
int x;
x = 10;
In this example, the variable x
is an int
and Java will initialize it to 0
for you. When you assign it the value of 10
on the second line, your value of 10
is written into the memory location referred to by x
.
在这个例子中,变量x
是 an int
,Java 会0
为你初始化它。当您10
在第二行为它分配值时,您的值将10
写入由 引用的内存位置x
。
But, when you try to declare a reference type, something different happens. Take the following code:
但是,当您尝试声明引用类型时,会发生一些不同的事情。取以下代码:
Integer num;
num = new Integer(10);
The first line declares a variable named num
, but it does not actually contain a primitive value yet. Instead, it contains a pointer (because the type is Integer
which is a reference type). Since you have not yet said what to point to, Java sets it to null
, which means "I am pointing to nothing".
第一行声明了一个名为 的变量num
,但它实际上并不包含原始值。相反,它包含一个指针(因为类型是Integer
引用类型)。由于您还没有说要指向什么,Java 将其设置为null
,这意味着“我没有指向任何内容”。
In the second line, the new
keyword is used to instantiate (or create) an object of type Integer
and the pointer variable num
is assigned to that Integer
object.
在第二行中,new
关键字用于实例化(或创建)一个类型的对象,Integer
并将指针变量num
分配给该Integer
对象。
The NullPointerException
occurs when you declare a variable but did not create an object and assign to the variable before trying to use the contents of the variable (called dereferencing). So you are pointing to something that does not actually exist.
NullPointerException
当您声明一个变量但未创建对象并在尝试使用变量的内容(称为取消引用)之前分配给该变量时,就会发生这种情况。所以你指的是一些实际上并不存在的东西。
Dereferencing usually happens when using .
to access a method or field, or using [
to index an array.
取消引用通常发生在.
用于访问方法或字段,或[
用于索引数组时。
If you attempt to dereference num
BEFORE creating the object you get a NullPointerException
. In the most trivial cases, the compiler will catch the problem and let you know that "num may not have been initialized
," but sometimes you may write code that does not directly create the object.
如果您尝试num
在创建对象之前取消引用,则会得到NullPointerException
. 在最琐碎的情况下,编译器会发现问题并让您知道“ num may not have been initialized
,”,但有时您可能会编写不直接创建对象的代码。
For instance, you may have a method as follows:
例如,您可能有如下方法:
public void doSomething(SomeObject obj) {
//do something to obj
}
In which case, you are not creating the object obj
, but rather assuming that it was created before the doSomething()
method was called. Note, it is possible to call the method like this:
在这种情况下,您不是在创建 object obj
,而是假设它是在doSomething()
调用方法之前创建的。请注意,可以像这样调用该方法:
doSomething(null);
In which case, obj
is null
. If the method is intended to do something to the passed-in object, it is appropriate to throw the NullPointerException
because it's a programmer error and the programmer will need that information for debugging purposes. Please include the name of the object variable in the exception message, like
在这种情况下,obj
是null
。如果该方法旨在对传入的对象执行某些操作,则抛出 是合适的,NullPointerException
因为这是一个程序员错误,程序员将需要该信息进行调试。请在异常消息中包含对象变量的名称,例如
Objects.requireNonNull(a, "a");
Alternatively, there may be cases where the purpose of the method is not solely to operate on the passed in object, and therefore a null parameter may be acceptable. In this case, you would need to check for a null parameterand behave differently. You should also explain this in the documentation. For example, doSomething()
could be written as:
或者,可能存在方法的目的不仅仅是对传入的对象进行操作的情况,因此空参数可能是可以接受的。在这种情况下,您需要检查空参数并采取不同的行为。您还应该在文档中解释这一点。例如,doSomething()
可以写成:
/**
* @param obj An optional foo for ____. May be null, in which case
* the result will be ____.
*/
public void doSomething(SomeObject obj) {
if(obj == null) {
//do something
} else {
//do something else
}
}
Finally, How to pinpoint the exception & cause using Stack Trace
What methods/tools can be used to determine the cause so that you stop the exception from causing the program to terminate prematurely?
可以使用哪些方法/工具来确定原因,以便阻止异常导致程序过早终止?
Sonar with findbugs can detect NPE. Can sonar catch null pointer exceptions caused by JVM Dynamically
带有 findbugs 的声纳可以检测 NPE。 sonar可以动态捕获JVM引起的空指针异常吗
回答by Bill the Lizard
NullPointerException
s are exceptions that occur when you try to use a reference that points to no location in memory (null) as though it were referencing an object. Calling a method on a null reference or trying to access a field of a null reference will trigger a NullPointerException
. These are the most common, but other ways are listed on the NullPointerException
javadoc page.
NullPointerException
s 是当您尝试使用指向内存中任何位置 (null) 的引用时发生的异常,就像它在引用一个对象一样。对空引用调用方法或尝试访问空引用的字段将触发NullPointerException
. 这些是最常见的,但其他方式列在NullPointerException
javadoc 页面上。
Probably the quickest example code I could come up with to illustrate a NullPointerException
would be:
我能想出的最快的示例代码来说明 aNullPointerException
可能是:
public class Example {
public static void main(String[] args) {
Object obj = null;
obj.hashCode();
}
}
On the first line inside main
, I'm explicitly setting the Object
reference obj
equal to null
. This means I have a reference, but it isn't pointing to any object. After that, I try to treat the reference as though it points to an object by calling a method on it. This results in a NullPointerException
because there is no code to execute in the location that the reference is pointing.
在里面的第一行main
,我明确地将Object
引用设置为obj
等于null
。这意味着我有一个引用,但它没有指向任何对象。在那之后,我尝试通过调用一个方法来将引用视为指向一个对象。这会导致 a,NullPointerException
因为在引用指向的位置没有要执行的代码。
(This is a technicality, but I think it bears mentioning: A reference that points to null isn't the same as a C pointer that points to an invalid memory location. A null pointer is literally not pointing anywhere, which is subtly different than pointing to a location that happens to be invalid.)
(这是一个技术问题,但我认为值得一提的是:指向 null 的引用与指向无效内存位置的 C 指针不同。空指针实际上不指向任何地方,这与指向一个碰巧无效的位置。)
回答by Jorge Ferreira
It's like you are trying to access an object which is null
. Consider below example:
这就像您试图访问一个对象,该对象是null
. 考虑下面的例子:
TypeA objA;
At this time you have just declaredthis object but not initialized or instantiated. And whenever you try to access any property or method in it, it will throw NullPointerException
which makes sense.
此时您刚刚声明了这个对象,但尚未初始化或实例化。并且每当您尝试访问其中的任何属性或方法时,它都会抛出 NullPointerException
这是有道理的。
See this below example as well:
请参见下面的示例:
String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
回答by MrZebra
A null
pointer is one that points to nowhere. When you dereference a pointer p
, you say "give me the data at the location stored in "p". When p
is a null
pointer, the location stored in p
is nowhere
, you're saying "give me the data at the location 'nowhere'". Obviously, it can't do this, so it throws a null pointer exception
.
一个null
指针是一个指向任何地方。当你取消引用一个指针时p
,你说“给我存储在“p”中的位置的数据。当p
是一个null
指针时,存储在p
is 中的位置nowhere
,你说“给我位置'无处'的数据”。显然,它不能这样做,所以它抛出一个null pointer exception
.
In general, it's because something hasn't been initialized properly.
通常,这是因为某些内容尚未正确初始化。
回答by OscarRyz
In Javaall the variables you declare are actually "references" to the objects (or primitives) and not the objects themselves.
在Java 中,您声明的所有变量实际上都是对对象(或原语)的“引用”,而不是对象本身。
When you attempt to execute one object method, the reference asks the living object to execute that method. But if the reference is referencing NULL (nothing, zero, void, nada) then there is no way the method gets executed. Then the runtime let you know this by throwing a NullPointerException.
当您尝试执行一个对象方法时,引用会要求活对象执行该方法。但是,如果引用引用 NULL(无、零、void、nada),则无法执行该方法。然后运行时通过抛出 NullPointerException 来让您知道这一点。
Your reference is "pointing" to null, thus "Null -> Pointer".
您的引用是“指向”空值,因此是“空值-> 指针”。
The object lives in the VM memory space and the only way to access it is using this
references. Take this example:
该对象位于 VM 内存空间中,访问它的唯一方法是使用this
引用。拿这个例子:
public class Some {
private int id;
public int getId(){
return this.id;
}
public setId( int newId ) {
this.id = newId;
}
}
And on another place in your code:
在你的代码的另一个地方:
Some reference = new Some(); // Point to a new object of type Some()
Some otherReference = null; // Initiallly this points to NULL
reference.setId( 1 ); // Execute setId method, now private var id is 1
System.out.println( reference.getId() ); // Prints 1 to the console
otherReference = reference // Now they both point to the only object.
reference = null; // "reference" now point to null.
// But "otherReference" still point to the "real" object so this print 1 too...
System.out.println( otherReference.getId() );
// Guess what will happen
System.out.println( reference.getId() ); // :S Throws NullPointerException because "reference" is pointing to NULL remember...
This an important thing to know - when there are no more references to an object (in the example above when reference
and otherReference
both point to null) then the object is "unreachable". There is no way we can work with it, so this object is ready to be garbage collected, and at some point, the VM will free the memory used by this object and will allocate another.
了解这一点很重要 - 当不再有对对象的引用时(在上面的示例中,当reference
和otherReference
都指向 null)时,该对象是“无法访问的”。我们无法使用它,因此该对象已准备好进行垃圾回收,并且在某个时刻,VM 将释放此对象使用的内存并分配另一个。
回答by ashish bhatt
In Java, everything (excluding primitive types) is in the form of a class.
在 Java 中,一切(不包括原始类型)都是以类的形式存在的。
If you want to use any object then you have two phases:
如果你想使用任何对象,那么你有两个阶段:
- Declare
- Initialization
- 宣布
- 初始化
Example:
例子:
- Declaration:
Object object;
- Initialization:
object = new Object();
- 宣言:
Object object;
- 初始化:
object = new Object();
Same for the array concept:
数组概念相同:
- Declaration:
Item item[] = new Item[5];
- Initialization:
item[0] = new Item();
- 宣言:
Item item[] = new Item[5];
- 初始化:
item[0] = new Item();
If you are not giving the initialization section then the NullPointerException
arise.
如果你没有给出初始化部分,那么NullPointerException
就会出现。
回答by nathan1138
A null pointer exception is thrown when an application attempts to use null in a case where an object is required. These include:
当应用程序在需要对象的情况下尝试使用 null 时,会抛出空指针异常。这些包括:
- Calling the instance method of a
null
object. - Accessing or modifying the field of a
null
object. - Taking the length of
null
as if it were an array. - Accessing or modifying the slots of
null
as if it were an array. - Throwing
null
as if it were a Throwable value.
- 调用
null
对象的实例方法。 - 访问或修改
null
对象的字段。 - 把 的长度
null
当作一个数组。 - 访问或修改插槽,
null
就好像它是一个数组一样。 - 投掷
null
就好像它是一个 Throwable 值。
Applications should throw instances of this class to indicate other illegal uses of the null
object.
应用程序应抛出此类的实例以指示该null
对象的其他非法使用。
Reference: http://docs.oracle.com/javase/8/docs/api/java/lang/NullPointerException.html
参考:http: //docs.oracle.com/javase/8/docs/api/java/lang/NullPointerException.html
回答by javid piprani
A null pointer exception is an indicator that you are using an object without initializing it.
空指针异常表明您正在使用对象而未对其进行初始化。
For example, below is a student class which will use it in our code.
例如,下面是一个学生类,它将在我们的代码中使用它。
public class Student {
private int id;
public int getId() {
return this.id;
}
public setId(int newId) {
this.id = newId;
}
}
The below code gives you a null pointer exception.
下面的代码为您提供了一个空指针异常。
public class School {
Student student;
public School() {
try {
student.getId();
}
catch(Exception e) {
System.out.println("Null pointer exception");
}
}
}
Because you are using student
, but you forgot to initialize it like in the
correct code shown below:
因为您正在使用student
,但是您忘记像下面显示的正确代码一样初始化它:
public class School {
Student student;
public School() {
try {
student = new Student();
student.setId(12);
student.getId();
}
catch(Exception e) {
System.out.println("Null pointer exception");
}
}
}
回答by Makoto
Another occurrence of a NullPointerException
occurs when one declares an object array, then immediately tries to dereference elements inside of it.
NullPointerException
当声明一个对象数组,然后立即尝试取消引用其中的元素时,会发生另一种情况。
String[] phrases = new String[10];
String keyPhrase = "Bird";
for(String phrase : phrases) {
System.out.println(phrase.equals(keyPhrase));
}
This particular NPE can be avoided if the comparison order is reversed; namely, use .equals
on a guaranteed non-null object.
如果颠倒比较顺序,就可以避免这种特殊的 NPE;即,.equals
在有保证的非空对象上使用。
All elements inside of an array are initialized to their common initial value; for any type of object array, that means that all elements are null
.
数组内的所有元素都被初始化为其共同的初始值;对于任何类型的对象数组,这意味着所有元素都是null
.
You mustinitialize the elements in the array beforeaccessing or dereferencing them.
您必须在访问或取消引用数组中的元素之前初始化它们。
String[] phrases = new String[] {"The bird", "A bird", "My bird", "Bird"};
String keyPhrase = "Bird";
for(String phrase : phrases) {
System.out.println(phrase.equals(keyPhrase));
}
回答by fgb
What is a NullPointerException?
什么是空指针异常?
A good place to start is the JavaDocs. They have this covered:
一个很好的起点是JavaDocs。他们涵盖了以下内容:
Thrown when an application attempts to use null in a case where an object is required. These include:
- Calling the instance method of a null object.
- Accessing or modifying the field of a null object.
- Taking the length of null as if it were an array.
- Accessing or modifying the slots of null as if it were an array.
- Throwing null as if it were a Throwable value.
Applications should throw instances of this class to indicate other illegal uses of the null object.
当应用程序在需要对象的情况下尝试使用 null 时抛出。这些包括:
- 调用空对象的实例方法。
- 访问或修改空对象的字段。
- 将 null 的长度当作一个数组。
- 访问或修改 null 的插槽,就好像它是一个数组一样。
- 将 null 视为 Throwable 值。
应用程序应抛出此类的实例以指示 null 对象的其他非法使用。
It is also the case that if you attempt to use a null reference with synchronized
, that will also throw this exception, per the JLS:
如果您尝试使用空引用 with synchronized
,也会抛出此异常,根据 JLS:
SynchronizedStatement: synchronized ( Expression ) Block
- Otherwise, if the value of the Expression is null, a
NullPointerException
is thrown.
SynchronizedStatement: synchronized ( Expression ) Block
- 否则,如果 Expression 的值为 null,
NullPointerException
则抛出 a。
How do I fix it?
我如何解决它?
So you have a NullPointerException
. How do you fix it? Let's take a simple example which throws a NullPointerException
:
所以你有一个NullPointerException
. 你如何解决它?让我们举一个简单的例子,它抛出一个NullPointerException
:
public class Printer {
private String name;
public void setName(String name) {
this.name = name;
}
public void print() {
printString(name);
}
private void printString(String s) {
System.out.println(s + " (" + s.length() + ")");
}
public static void main(String[] args) {
Printer printer = new Printer();
printer.print();
}
}
Identify the null values
识别空值
The first step is identifying exactly which values are causing the exception. For this, we need to do some debugging. It's important to learn to read a stacktrace. This will show you where the exception was thrown:
第一步是准确识别导致异常的值。为此,我们需要进行一些调试。学习阅读stacktrace很重要。这将显示抛出异常的位置:
Exception in thread "main" java.lang.NullPointerException
at Printer.printString(Printer.java:13)
at Printer.print(Printer.java:9)
at Printer.main(Printer.java:19)
Here, we see that the exception is thrown on line 13 (in the printString
method). Look at the line and check which values are null by
adding logging statementsor using a debugger. We find out that s
is null, and calling the length
method on it throws the exception. We can see that the program stops throwing the exception when s.length()
is removed from the method.
在这里,我们看到异常是在第 13 行(在printString
方法中)抛出的。查看该行并通过添加日志记录语句或使用调试器检查哪些值为空。我们发现它s
是空的,调用length
它的方法会抛出异常。我们可以看到程序在s.length()
从方法中删除时停止抛出异常。
Trace where these values come from
追踪这些值的来源
Next check where this value comes from. By following the callers of the method, we see that s
is passed in with printString(name)
in the print()
method, and this.name
is null.
接下来检查这个值的来源。按照该方法的调用者,我们看到,s
在用传递printString(name)
的print()
方法,并this.name
为空。
Trace where these values should be set
跟踪应设置这些值的位置
Where is this.name
set? In the setName(String)
method. With some more debugging, we can see that this method isn't called at all. If the method was called, make sure to check the orderthat these methods are called, and the set method isn't called afterthe print method.
在哪里this.name
设置?在setName(String)
方法中。通过更多的调试,我们可以看到这个方法根本没有被调用。如果调用了该方法,请确保检查这些方法的调用顺序,并且 set 方法不是在print 方法之后调用的。
This is enough to give us a solution: add a call to printer.setName()
before calling printer.print()
.
这足以给我们一个解决方案:在调用printer.setName()
之前添加一个调用printer.print()
。
Other fixes
其他修复
The variable can have a default value(and setName
can prevent it being set to null):
变量可以有一个默认值(并且setName
可以防止它被设置为 null):
private String name = "";
Either the print
or printString
method can check for null, for example:
任一print
或printString
方法可以检查空,例如:
printString((name == null) ? "" : name);
Or you can design the class so that name
always has a non-null value:
或者您可以设计该类,使其name
始终具有非空值:
public class Printer {
private final String name;
public Printer(String name) {
this.name = Objects.requireNonNull(name);
}
public void print() {
printString(name);
}
private void printString(String s) {
System.out.println(s + " (" + s.length() + ")");
}
public static void main(String[] args) {
Printer printer = new Printer("123");
printer.print();
}
}
See also:
也可以看看:
I still can't find the problem
我还是找不到问题
If you tried to debug the problem and still don't have a solution, you can post a question for more help, but make sure to include what you've tried so far. At a minimum, include the stacktracein the question, and mark the important line numbersin the code. Also, try simplifying the code first (see SSCCE).
如果您尝试调试问题但仍然没有解决方案,您可以发布问题以获得更多帮助,但请确保包括您迄今为止尝试过的内容。至少,在问题中包含堆栈跟踪,并在代码中标记重要的行号。另外,请先尝试简化代码(请参阅SSCCE)。