什么是NullPointerException,我该如何解决? [英] What is a NullPointerException, and how do I fix it?

查看:177
本文介绍了什么是NullPointerException,我该如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是空指针异常( java.lang.NullPointerException )及其原因是什么?

What are Null Pointer Exceptions (java.lang.NullPointerException) and what causes them?

有哪些方法/ tools可用于确定原因,以便您停止异常导致程序提前终止?

What methods/tools can be used to determine the cause so that you stop the exception from causing the program to terminate prematurely?

推荐答案

当你声明一个引用变量(即一个对象),你实际上是在创建一个指向对象的指针。请考虑以下代码,其中声明基本类型的变量 int

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 x;
x = 10;

在这个例子中,变量x是 int 并且Java会将它初始化为0。当您在第二行中将其分配给10时,您的值10将被写入x指向的内存位置。

In this example, the variable x is an int and Java will initialize it to 0 for you. When you assign it to 10 in the second line your value 10 is written into the memory location pointed to by x.

但是,当您尝试声明引用类型时不同的事情。请使用以下代码:

But, when you try to declare a reference type something different happens. Take the following code:

Integer num;
num = new Integer(10);

第一行声明一个名为 num 的变量,但是,它不包含原始值。相反,它包含一个指针(因为类型是 Integer ,这是一个引用类型)。既然你还没有说什么指向Java将其设置为null,意味着我指向什么

The first line declares a variable named num, but, it does not contain a primitive value. Instead, it contains a pointer (because the type is Integer which is a reference type). Since you did not say as yet what to point to Java sets it to null, meaning "I am pointing at nothing".

在第二行, new 关键字用于实例化(或创建)Integer类型的对象和指针变量 num 被赋予这个对象。您现在可以使用解除引用运算符(一个点)来引用该对象。

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 this object. You can now reference the object using the dereferencing operator . (a dot).

您提出的异常是在声明变量但未创建对象时发生的。如果您尝试取消引用 num 在创建对象之前,您将获得 NullPointerException 。在最琐碎的情况下,编译器将捕获问题并让您知道num可能尚未初始化,但有时您编写的代码不会直接创建对象。

The Exception that you asked about occurs when you declare a variable but did not create an object. 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 write code that does not directly create the object.

例如,您可能有如下方法:

For instance, you may have a method as follows:

public void doSomething(SomeObject obj) {
   //do something to obj
}

在这种情况下你不是在创建对象 obj ,而不是假设它是在调用 doSomething 方法之前创建的。不幸的是,可以这样调用方法:

In which case you are not creating the object obj, rather assuming that it was created before the doSomething method was called. Unfortunately, it is possible to call the method like this:

doSomething(null);

在这种情况下, obj 为空。如果该方法旨在对传入的对象执行某些操作,则抛出 NullPointerException 是合适的,因为它是程序员错误,程序员将需要该信息用于调试目的。

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.

或者,可能存在这样的情况:方法的目的不仅仅是对传入的对象进行操作,因此可以接受空参数。在这种情况下,您需要检查 null参数并采取不同的行为。您还应该在文档中解释这一点。例如, doSomething 可以写成:

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 parameter and behave differently. You should also explain this in the documentation. For example, doSomething could be written as:

/**
  * @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
    }
}

最后,如何查明异常&使用堆栈跟踪

这篇关于什么是NullPointerException,我该如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆