NullPointerException in array

后端 未结 2 903
长情又很酷
长情又很酷 2021-01-29 12:20

I keep getting a NullPointerException at (see below). Everything works fine in C#, but in android it breaks?

arrDBNumbers is full and code is supposed to run through an

2条回答
  •  耶瑟儿~
    2021-01-29 12:52

    Because just writing

    Integer[][] arrFreq = new Integer[49][2];
    

    means you have initialized the array with all null elements because it is an array of Integer Objects and Object's default value will be null reference. Hence,

    arrFreq[i][1]++;  // trying null++;
    

    gives NullPointerException.

    This wouldn't have been the case if you had used an array of primitives, which will default to an array of 0s.

    int[][] arrFreq = new int[49][2];
    

提交回复
热议问题