为什么 Java 会在这里抛出 NullPointerException?

Why does Java throw NullPointerException here?(为什么 Java 会在这里抛出 NullPointerException?)
本文介绍了为什么 Java 会在这里抛出 NullPointerException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Test {

    public int [] x;

    public Test(int N)
    {
       int[] x = new int [N];
       for (int i=0;i<x.length;i++)
       {
           x[i]=i;
           StdOut.println(x[i]);
       }
    }


    public static void main(String[] args) { 

        String path = "/Users/alekscooper/Desktop/test.txt";
        In reader = new In(path);
        int size=reader.readInt();
        StdOut.println("Size = "+size);

        Test N = new Test(size);
        StdOut.println(N.x[3]);

    }

    /* ADD YOUR CODE HERE */

}

大家好.我正在通过阅读 Robert Sedgwick 的算法书来学习 Java,并且我正在使用他的库,例如 StdOut.但问题一般是关于 Java 的.我不明白为什么这里的 Java 会抛出 NullPointerException.我确实知道这通常意味着什么,但我不知道它为什么会出现在这里,因为这就是我认为我正在做的事情:

Hello guys. I'm learning Java through reading Robert Sedgwick's book on algorithms and I'm using his libraries such as StdOut, for example. But the question is about Java in general. I don't understand why Java here throws a NullPointerException. I do know what that means in general, but I don't know why it is here because here's what I think I'm doing:

  1. 从文件中读取一个整数——数组的大小在类测试中.在我的测试示例中 size=10,所以不会发生超出范围的事情.

  1. read an integer number from the file - the size of the array in the class Test. In my test example size=10, so no out-of-bound type of thing happens.

打印出来.

创建Test类型的对象N.在这个对象中,我想我创建了一个我刚刚拥有的大小数组从文件中读取.为了好玩,我将它从 0 初始化为 size-1 和打印它.到目前为止一切顺利.

create the object N of type Test. In this object I think I create an array of size that I have just read from the file. For fun I initialize it from 0 to size-1 and print it. So far so good.

一切从这里开始.因为我的课是公开的,所以我跑了构造函数我认为我有对象 N 作为属性具有带有 size 个元素的数组 x.但是,当我尝试例如,寻址 x,

and here where it all begins. Since my class is public and I've run the constructor I think I have the object N which as an attribute has the array x with size elements. However, when I'm trying to address x, for example,

StdOut.println(N.x[3]);

StdOut.println(N.x[3]);

Java 抛出 NullPointerException.

Java throws NullPointerException.

为什么会这样?请提供帮助,非常感谢您抽出宝贵时间.

Why so? Please help and thank you very much for your time.

推荐答案

你所做的被称为 shadowing 你用局部变量 x<隐藏了你的字段 x/代码>.所以你需要做的就是避免这种情况:

what you did is called shadowing you shadowed your field x with local variable x. so all you need to do is avoiding this:

int[] x = new int [N]; 是错误的,如果您希望字段初始化而不是局部变量,那么您可以执行以下操作: x = new int[N]; 更多信息请阅读 this

int[] x = new int [N]; is wrong, if you want your field to initialize instead of a local variable then you could do something like : x = new int [N]; for more information read this

这篇关于为什么 Java 会在这里抛出 NullPointerException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How can create a producer using Spring Cloud Kafka Stream 3.1(如何使用Spring Cloud Kafka Stream 3.1创建制片人)
Insert a position in a linked list Java(在链接列表中插入位置Java)
Did I write this constructor properly?(我是否正确地编写了这个构造函数?)
Head value set to null but tail value still gets displayed(Head值设置为空,但仍显示Tail值)
printing nodes from a singly-linked list(打印单链接列表中的节点)
Control namespace prefixes in web services?(控制Web服务中的命名空间前缀?)