安卓:发行期间ArrayList的声明 [英] Android: Issue during Arraylist declaration

查看:234
本文介绍了安卓:发行期间ArrayList的声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我宣布ArrayList中像这样 -

If I declare Arraylist like this-

private ArrayList<Integer[]> nodeList;

那么,同时加入数组进去,得到NullPointerException异常

then, while adding array into it, getting NullPointerException

但是,如果我把它改为 -

But, if I change it to-

private ArrayList<Integer[]> nodeList= new ArrayList<Integer[]>();

- 它工作得很好。

-it works fine.

为什么第一个失败!

推荐答案

第一只声明了一个变量,但不会产生实际的对象。只有当你使用,您实际创建的对象。

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

在的Java与C ++,声明一个变量不分配它的局部变量。要真正创建对象,你需要明确创建它[在你的榜样:通过使用关键字。
(*)请注意,这是唯一真正要的引用类型的对象,和Java 与申报创建。

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.

这篇关于安卓:发行期间ArrayList的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆