java空指针异常:问题的根源和解决思路

🌌 365比分官网 ⏱️ 2025-09-25 02:13:31 👤 admin 👁️ 3409 ⭐ 385
java空指针异常:问题的根源和解决思路

@Java空指针异常 Exception in thread “main” java.lang.NullPointerException 前一段时间写实验,遇到了一个java空指针异常的问题,经过多方查询资料,中越解决了异常,因此决定记录一下本次异常的解决。

遇到的问题

程序的基本思想是读取文件,逐个将单词分开,将单词封装至Word类,然后判断单词的类型,将类型和单词本身写入输出文件中。但是这一句

if (words[i].word !=null) {

出現了空指针异常,我多次调试,总是找不到问题的根源在哪里。

解决的方法

搜索问题的时候,忽然看到了这篇文章,给我了启发 https://www.sohu.com/a/208264500_611601 文章中 这一句让我忽然意识到,是不是因为我的words[i]是一个null对象,于是我将代码修改成

if (words[i]!=null&&words[i].word !=null) {

也就是先判断words[i]这个对象是否为空,在进行words[i]的属性的判断,运行之后果然成功解决了。

查看源码

本着追本溯源的想法,想翻看对应的源码,看看还有什么原因可能会造成空指针。NullPointerException的源码如下:

/*

* Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.

* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*/

package java.lang;

/**

* Thrown when an application attempts to use {@code null} in a

* case where an object is required. These include:

*

    *

  • Calling the instance method of a {@code null} object.

    *

  • Accessing or modifying the field of a {@code null} object.

    *

  • Taking the length of {@code null} as if it were an array.

    *

  • Accessing or modifying the slots of {@code null} as if it

    * were an array.

    *

  • Throwing {@code null} as if it were a {@code Throwable}

    * value.

    *

*

* Applications should throw instances of this class to indicate

* other illegal uses of the {@code null} object.

*

* {@code NullPointerException} objects may be constructed by the

* virtual machine as if {@linkplain Throwable#Throwable(String,

* Throwable, boolean, boolean) suppression were disabled and/or the

* stack trace was not writable}.

*

* @author unascribed

* @since JDK1.0

*/

public

class NullPointerException extends RuntimeException {

private static final long serialVersionUID = 5162710183389028792L;

/**

* Constructs a {@code NullPointerException} with no detail message.

*/

public NullPointerException() {

super();

}

/**

* Constructs a {@code NullPointerException} with the specified

* detail message.

*

* @param s the detail message.

*/

public NullPointerException(String s) {

super(s);

}

}

其中

1.

    2.

  • Calling the instance method of a {@code null} object.

    3.

  • Accessing or modifying the field of a {@code null} object.

    4.

  • Taking the length of {@code null} as if it were an array.

    5.

  • Accessing or modifying the slots of {@code null} as if it

    6. were an array.

    7.

  • Throwing {@code null} as if it were a {@code Throwable}

    8. value.

    9.

不难看出,当以上五种情况的时候,会报空指针异常。 大致可以翻译为:

1.调用空对象的实例 2.访问或者修改空对象 3.测试一个空对象数组的长度 4.访问或者修改一个空数组的插槽(?水平有限,不太懂slots) 5.把一个空值当做抛出的参数

也就是说,避免出现空指针异常,要避免出现上述的五种情况才行。在很多规范编码的时候可以避免空指针异常。例如alibaba的编码规范中,一般在调用string1.equals(string2)的时候,如果string2是一个具体的字符串,而string1是一个对象的属性,此时应该把euqal反转,写成string2.euqals(string1). 例如,当你在idea中安装了编码规范,而写成了可能导致空指针的形式。idea就会提醒你 此时,应该把改成

"true".equals(resultItems.get("type")

这样就会避免可能会导致空指针的问题。

总结

遇到问题的时候,不要慌,多查查,多思考。大多数能够遇到的问题,应该都有前辈已经遇到过了,所以要多查,发现错误,吸取经验。

多看看源码,很多异常源码里面都有说明。

java源码链接:源码

🛸 相关文章

怎么把显示器调到全屏
365比分官网

怎么把显示器调到全屏

📅 08-27 👁️ 644
站队用英语怎么说
注册office365邮箱

站队用英语怎么说

📅 07-30 👁️ 1865