在java中将arraylist传输到double array[0]
我有这样的代码:
public class Test{
arrayList<String> list = new ArrayList<String>();
String[][] temp_list;
public static void main(String[] args)
{
String temp = list.get(0);
temp_list[0] = temp.split(" ");
}
}
我想将“列表”中的第一项传输到 temp_list[0] 中。编译成功,但运行时出现错误。这就是错误:
Exception in thread "main" java.lang.NullPointerException
at Test.main(Test.java:this line=>temp_list[0] = temp.split(" ");)
任何人都可以帮助我吗?
i have this code:
public class Test{
arrayList<String> list = new ArrayList<String>();
String[][] temp_list;
public static void main(String[] args)
{
String temp = list.get(0);
temp_list[0] = temp.split(" ");
}
}
i want to transfer the first item in 'list' into temp_list[0].compiling is success but i got error when i run it.this is the error:
Exception in thread "main" java.lang.NullPointerException
at Test.main(Test.java:this line=>temp_list[0] = temp.split(" ");)
anyone can help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为您尚未为
temp_list
分配任何二维数组。 (分割的结果应该存储在哪个数组中?)这是代码片段的工作版本。
This is because you haven't allocated any 2D-array for
temp_list
. (Which array should the result of split be stored in?)Here's a working version of your snippet.
该代码将无法编译,因为 list 被声明为类的成员变量,但 main 是静态方法。
正如所写, list 也没有添加任何内容,因此对 list.get(0) 的调用将抛出异常(尽管不是空指针)。
在给定的代码中,数组 temp_list 未分配(没有新的),因此尝试对其进行分配将引发空指针异常。
This code would will not compile since list is declared as a member variable of the class but main is a static method.
As written, list has nothing added too so the call to list.get(0) will throw an Exception (not null pointer though).
The array temp_list is not allocated (no new) in the code given so trying assign into it will throw a null pointer exception.
使用之前需要先初始化temp_list。您需要指定数组的大小。例如:
You need to initialize temp_list before you use it. You need to specify the size of the array. For example: