Android: Issue during Arraylist declaration

前端 未结 1 1441
终归单人心
终归单人心 2021-01-28 01:40

If I declare Arraylist like this-

private ArrayList nodeList;

then, while adding array into it, getting NullPointerException

1条回答
  •  孤独总比滥情好
    2021-01-28 02:22

    The first only declares a variable, but does not create the actual object. only when you use new, you actually create the object.

    In java unlike C++, declaring a variable does not allocate a local variable of it. To actually create the object, you need to explicitly create it [in your example: by using the new keyword].
    (*)Note that this is only true to reference types objects, and java primitives are created with declaration.

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