Java 什么是NullPointerException,如何修复它?
什么是空指针异常(Java 什么是NullPointerException,如何修复它?,java,nullpointerexception,Java,Nullpointerexception,什么是空指针异常(java.lang.NullPointerException)以及是什么导致了它们 String a = null; System.out.println(a.toString()); // NullPointerException will be thrown 可以使用哪些方法/工具来确定原因,从而阻止异常导致程序过早终止?NullPointerExceptions是当您尝试使用指向内存中无位置(null)的引用时发生的异常,就好像它在引用对象一样。对空引用调用方法或尝试访
java.lang.NullPointerException
)以及是什么导致了它们
String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
可以使用哪些方法/工具来确定原因,从而阻止异常导致程序过早终止?NullPointerExceptions是当您尝试使用指向内存中无位置(null)的引用时发生的异常,就好像它在引用对象一样。对空引用调用方法或尝试访问空引用的字段将触发
NullPointerException
。这些是最常见的,但javadoc页面上列出了其他方法
String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
可能我能想出的最快的示例代码来说明NullPointerException
:
public class Example {
public static void main(String[] args) {
Object obj = null;
obj.hashCode();
}
}
String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
在main
内的第一行,我将对象
referenceobj
显式设置为null
。这意味着我有一个引用,但它没有指向任何对象。之后,我尝试通过调用对象上的方法,将引用视为指向对象。这会导致NullPointerException
,因为在引用所指向的位置没有要执行的代码
String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
(这是一个技术问题,但我认为值得一提:指向null的引用与指向无效内存位置的C指针不同。null指针实际上不指向任何地方,这与指向恰好无效的位置略有不同。)
NullPointerException
s是当您试图使用指向内存中无位置(null)的引用时发生的异常,就像它在引用对象一样。对空引用调用方法或尝试访问空引用的字段将触发NullPointerException
。这些是最常见的,但javadoc页面上列出了其他方法
String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
可能我能想出的最快的示例代码来说明NullPointerException
:
public class Example {
public static void main(String[] args) {
Object obj = null;
obj.hashCode();
}
}
String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
在main
内的第一行,我将对象
referenceobj
显式设置为null
。这意味着我有一个引用,但它没有指向任何对象。之后,我尝试通过调用对象上的方法,将引用视为指向对象。这会导致NullPointerException
,因为在引用所指向的位置没有要执行的代码
String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
(这是一个技术问题,但我认为值得一提:指向null的引用与指向无效内存位置的C指针不同。null指针实际上不指向任何地方,这与指向恰好无效的位置略有不同。)这就像你试图访问一个
为空的对象一样。考虑下面的例子:
TypeA objA;
String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
此时,您刚刚声明了该对象,但未初始化或实例化该对象。每当您试图访问其中的任何属性或方法时,它都会抛出有意义的NullPointerException
String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
请参见下面的示例:
String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
这就像您试图访问的对象是null
。考虑下面的例子:
TypeA objA;
String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
此时,您刚刚声明了该对象,但未初始化或实例化该对象。每当您试图访问其中的任何属性或方法时,它都会抛出有意义的NullPointerException
String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
请参见下面的示例:
String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
null
指针不指向任何地方。当您取消引用指针p
时,您说“将存储在“p”中的位置的数据提供给我”。当p
是null
指针时,存储在p
中的位置是无处
,您说的是“将“无处”位置的数据提供给我”“。显然,它不能这样做,因此它会抛出一个空指针异常
String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
一般来说,这是因为某些东西没有正确初始化。null
指针是一个不指向任何地方的指针。当您取消引用指针p
时,您说“将存储在“p”中的位置的数据提供给我”。当p
是null
指针时,存储在p
中的位置是无处
,您说的是“将“无处”位置的数据提供给我”“。显然,它不能这样做,因此它会抛出一个空指针异常
String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
一般来说,这是因为某些东西没有正确初始化。当您声明引用变量(即对象)时,实际上是在创建指向对象的指针。考虑下面的代码,其中声明一个原始类型的变量<代码> int <代码>:
String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
int x;
x = 10;
在本例中,变量x
是一个int
,Java将为您将其初始化为0
。当您在第二行指定10
的值时,10
的值将写入x
引用的内存位置
String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
但是,当您试图声明引用类型时,会发生一些不同的情况。以下面的代码为例:
String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
Integer num;
num = new Integer(10);
第一行声明了一个名为num
的变量,但它实际上还没有包含基元值。相反,它包含一个指针(因为类型是引用类型的Integer
)。由于您还没有说要指向什么,Java将其设置为null
,这意味着“我没有指向任何东西”
String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
在第二行中,使用new
关键字实例化(或创建)类型为Integer
的对象,指针变量num
分配给该Integer
对象
String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
NullPointerException
(NPE)发生在
int length = foo.length(); // HERE
public class Test {
private static String[] foo = new String[2];
private static int test(String[] bar, int pos) {
return bar[pos].length();
}
public static void main(String[] args) {
int length = test(foo, 1);
}
}
$ javac Test.java
$ java Test
Exception in thread "main" java.lang.NullPointerException
at Test.test(Test.java:6)
at Test.main(Test.java:10)
$
return args[pos].length();
int length = test(foo, 1);
private static String[] foo = new String[2];