NullPointerException in array

后端 未结 2 877
长情又很酷
长情又很酷 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];
    
    0 讨论(0)
  • 2021-01-29 13:01

    Integer is a container for int, in your case you have lots of null objects. if you use int instead it will work as you expect.

    0 讨论(0)
提交回复
热议问题