Why does Java throw NullPointerException here?

后端 未结 5 2003
半阙折子戏
半阙折子戏 2020-11-30 15:09
public class Test {

    public int [] x;

    public Test(int N)
    {
       int[] x = new int [N];
       for (int i=0;i

        
5条回答
  •  既然无缘
    2020-11-30 15:22

    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]; 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

提交回复
热议问题