初始化数组和类方法测试阵列时后NullPointerException异常 - Java的 [英] Java - NullPointerException after initializing array and when testing array with class method

查看:207
本文介绍了初始化数组和类方法测试阵列时后NullPointerException异常 - Java的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图初始化数组,然后用一个类的方法测试其值。我已经初始化数组,并已经在构造函数中进行了测试成功,所以它看起来像阵列填充,但只要我尝试在主要的方法来测试这个它抛出一个空指针异常。

 类PercolGrid {
    双方诠释;
    布尔[]网格;    公共PercolGrid(INT内侧){
        双方=内侧;
        //格=新的布尔[] {假的,假的,假的,假的,假的,
        //假的,假的,假的,假的,假};
        布尔[] =电网新的布尔[两岸]
        的for(int i = 0; I<两侧;我++){
            电网[I] = FALSE;
            的System.out.println(设置+ I +到+电网[I]);
        }
        的System.out.println(检查之外循环,第一方阵是:+电网[0]);
    }
    公共布尔testSqr(int i)以{
        的System.out.println(请求的指标是:+ I);
        回格[I]
    }    公共静态无效的主要(字串[] args){
        PercolGrid firstGrid =新PercolGrid(10);
        的System.out.println(网格创建检查指标....!);
        的System.out.println(第一方阵是:+ firstGrid.testSqr(0)); // 空指针异常
        的System.out.println(第一方阵是:+ firstGrid.grid [0]); // 和这里
    }
}

这几乎就像在构造函数中存在引用的数据,但随后在它之外不存在。当我注释掉循环,它上面的布尔[] ... 行,并取消我的电网=新的布尔[] ... 行,这一切工作正常,但我想选边站的数量,当我实例化对象。

编辑 - 如果我注释掉,会出现此相同的错误行19( firstGrid.testSqr(0)),并改为运行线20条( firstGrid。电网[0] )。

这是使用一维数组之前,我尝试用二维数组一样的东西的做法。我缺少什么?

我的输出是这样的:

  0设置为false
...
9设置为false
检查外FOR循环,第一方阵的是:假的
网格创建!检查指标....
请求的指标为:0
显示java.lang.NullPointerException
    在PercolGrid.testSqr(PercolGrid.java:19)
    在PercolGrid.main(PercolGrid.java:25)


解决方案

您的问题是这一行:

 布尔[] =电网新的布尔[两岸]

这是初始化一个局部变量格,而不是字段中的实例。

将其更改为:

 电网=新的布尔[两岸]

这初始化领域的实例。

通过将类型在前面您声明一个新的变量。当在方法declar一个变量的范围仅限于该方法。由于本地变量​​命名为与您的实例变量是隐藏的实例变量。

I've been trying to initialize an array and then test its values with a class method. I have initialized the array and have already tested it successfully within the constructor, so it looks like the array was populated, but as soon as I try to test this in the Main method it's throwing a NullPointer exception.

class PercolGrid {
    int sides;
    boolean[] grid;

    public PercolGrid (int inSides) {
        sides = inSides;
        //grid = new boolean[] {false,false,false,false,false,
        //false,false,false,false,false};
        boolean[] grid = new boolean[sides];
        for (int i = 0; i < sides; i++) {
            grid[i] = false;
            System.out.println("Setting " + i + " to " + grid[i]);
        }
        System.out.println("Checking outside FOR loop, first square is: " + grid[0]);
    }


    public boolean testSqr (int i) {
        System.out.println("Requested index is: " + i);
        return grid[i];
    }

    public static void main(String[] args){
        PercolGrid firstGrid = new PercolGrid(10);
        System.out.println("Grid created! Checking index ....");
        System.out.println("First square is :" + firstGrid.testSqr(0)); // NullPointerException
        System.out.println("First square is :" + firstGrid.grid[0]); // and here
    }
}

It's almost like the referenced data exists within the constructor but then doesn't exist outside of it. When I comment out the for loop and the boolean[] .... line above it, and uncomment my grid = new boolean[] .... line, it all works ok, but I want to choose the number of sides when I instantiate the object.

EDIT - This same error occurs if I comment out line 19 (firstGrid.testSqr(0)) and instead run line 20 (firstGrid.grid[0]).

This is a practice using a 1D array before I try the same thing with a 2D array. What am I missing?

My output looks like this:

Setting 0 to false
...
Setting 9 to false
Checking outside FOR loop, first square is: false
Grid created! Checking index ....
Requested index is: 0
java.lang.NullPointerException
    at PercolGrid.testSqr(PercolGrid.java:19)
    at PercolGrid.main(PercolGrid.java:25)

解决方案

Your problem is with this line:

boolean[] grid = new boolean[sides];

This is initializing a local variable grid, not the field in the instance.

Change it to:

grid = new boolean[sides];

This initializes the field in the instance.

By putting the type in front you are declaring a new variable. When you declar a variable in a method its scope is limited to that method. Since your local variable is named the same as your instance variable it "hides" the instance variable.

这篇关于初始化数组和类方法测试阵列时后NullPointerException异常 - Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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