Why does Java throw NullPointerException here?

后端 未结 5 2012
半阙折子戏
半阙折子戏 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:28

    Inside public Test(int n):

    Change

    int[] x = new int [N]; // Creating a local int array x
    

    to

    x = new int [N]; // Assigning it to x
    

提交回复
热议问题