Java - NullPointerException in Array

前端 未结 1 621
独厮守ぢ
独厮守ぢ 2020-12-12 03:44

I have encountered the following problem: I have a java class with a private member like so:

private Arcs[] arcs;

This is not initialised i

相关标签:
1条回答
  • 2020-12-12 04:15

    Are you actually ever storing an Arcs object in arcs[i]? If not, all elements of arcs[] will be initialized to null. (Hence the NPE)

    Do something like this:

    while(condition){
        // ...
        arcs[i] = new Arcs();
        arcs[i].add(blah); 
        // ...
    }
    

    Reference:

    • Java Tutorial: Arrays
    0 讨论(0)
提交回复
热议问题