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:
 * <ul>
 * <li>Calling the instance method of a {@code null} object.
 * <li>Accessing or modifying the field of a {@code null} object.
 * <li>Taking the length of {@code null} as if it were an array.
 * <li>Accessing or modifying the slots of {@code null} as if it
 *     were an array.
 * <li>Throwing {@code null} as if it were a {@code Throwable}
 *     value.
 * </ul>
 * <p>
 * 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. <ul>
 2. <li>Calling the instance method of a {@code null} object.
 3. <li>Accessing or modifying the field of a {@code null} object.
 4. <li>Taking the length of {@code null} as if it were an array.
 5. <li>Accessing or modifying the slots of {@code null} as if it
 6.     were an array.
 7. <li>Throwing {@code null} as if it were a {@code Throwable}
 8.     value.
 9. </ul>

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

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

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

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

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

总结

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

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

  3. java源码链接:源码

  • 13
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值