Java:ListA.addAll(ListB) 触发 NullPointerException?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2793004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: ListA.addAll(ListB) fires NullPointerException?
提问by hhh
The err part is Capitalized in the code, it also comes in foreaching. Because of the abstract list, it cannot be initialized, declaration is in a static field. The lists have the same type.
代码中的 err 部分是大写的,它也出现在 foreaching 中。由于是抽象列表,它不能被初始化,声明在一个静态字段中。列表具有相同的类型。
import java.util.*;
public class Test
{
public static final List<String> highPrio = Arrays.asList("*","/");
public static List<String> ops;
public static void main(String[] args)
{
//ERROR HERE, why do it throw nullPointer?
ops.addAll(highPrio);
for(String s : ops)
{
System.out.println(s);
}
}
}
Why not new List() in the initialization?
为什么在初始化中没有 new List() ?
The reason for not initialization was the inability to use = new List<String>()
. I cannot see a logic not allowing it. It must have something to do with intrinsic factors such as data strucs or something else.
未初始化的原因是无法使用= new List<String>()
. 我看不到不允许这样做的逻辑。它必须与内在因素有关,例如数据结构或其他东西。
Test.java:7: java.util.List is abstract; cannot be instantiated
public static List<String> ops = new List<String>();
Test.java:7: java.util.List is abstract; cannot be instantiated
public static List<String> ops = new List<String>();
Why list is an interface?
为什么列表是一个接口?
I know that many data strucs such as stack implements list. But I cannot understand why List is an interface and why not Table for example. I see list as a primitive structure with which you can implement other structures. Interface is a thing where you can specify requirements for a structure. Is the primitivenness or extensivenss reason for being an interface?
我知道许多数据结构(例如堆栈)都实现了列表。但我不明白为什么 List 是一个接口,为什么不是 Table 。我将列表视为一种原始结构,您可以使用它来实现其他结构。接口是您可以指定结构要求的东西。作为接口的原因是原始性还是扩展性?
采纳答案by Matthew Flaschen
Because ops is null. The fact that List is an interface does not mean you can't initialize the field:
因为 ops 为空。List 是一个接口的事实并不意味着您不能初始化该字段:
public static List<String> ops = new ArrayList<String>();
List
is an interface because there are multiple ways of implementing it while providing the same contract (though different performance characteristics). For instance, ArrayList
is array-backed, while LinkedList
is a linked list.
List
是一个接口,因为在提供相同的契约的同时有多种实现它的方法(尽管不同的性能特征)。例如,ArrayList
是数组支持的,LinkedList
而是链表。
回答by Rob Goodwin
You need to instantiate the ops list.
您需要实例化操作列表。
public static List<String> ops = new ArrayList<String>();
or another list type of your choosing.
或您选择的其他列表类型。
回答by Thomas Sidoti
ops is never initialized.
ops 永远不会被初始化。
You have to do ops = new ArrayList<String>();
before you do the addAll command. Otherwise you are calling a null object.
您必须在执行ops = new ArrayList<String>();
addAll 命令之前执行此操作。否则你正在调用一个空对象。
The reason that you can't do ops = new List<String>
' is because List is an interface and cannot be initialized. ArrayList is not an abstract type and extends List, so it is appropriate in this context.
你不能做ops = new List<String>
'的原因是因为 List 是一个接口,不能被初始化。ArrayList 不是抽象类型并且扩展了 List,因此它适用于这种情况。
Abstract types and interfaces cannot be created as an actual object, and can only be used to reference some concrete object. The concrete type must extend the abstract type or interface which you are trying to use.
抽象类型和接口不能创建为实际对象,只能用于引用某些具体对象。具体类型必须扩展您尝试使用的抽象类型或接口。
回答by lucasweb
You have not initialized List ops;
你还没有初始化 List ops;
e.g.
例如
public static List<String> ops = new ArrayList<String>();
Alternatively you could do
或者你可以做
import java.util.*;
public class Test
{
public static List<String> ops;
public static void main(String[] args)
{
ops = Arrays.asList(new String[] {"*", "/"});
for(String s : ops)
{
System.out.println(s);
}
}
}
回答by KevinS
ops hasn't been initialized yet.
ops 还没有初始化。
change declaration to:
将声明更改为:
public static List<String> ops = new ArrayList<String>();
回答by fastcodejava
You are adding to what? Variable ops
is still null, just like s
is null in the following :
你在添加什么?变量ops
仍然为空,就像s
下面的为空:
public static String s;
回答by Bert F
I think your real question is:
我认为你真正的问题是:
Why can't I instantiate a List?
or Why am I getting: Test.java:7: java.util.List is abstract; cannot be instantiated public static List<String> ops = new List<String>();
为什么我不能实例化一个列表?
或为什么我得到:Test.java:7: java.util.List is abstract; cannot be instantiated public static List<String> ops = new List<String>();
In Java, List is an interface, which is, as you say: Interface is a thing where you can specify requirements for a structure
- its like a job description that needs to be filled. But you can't clip the job description out of the paper and simply use it to do the job, i.e. the List
job description is abstract; cannot be instantiated
.
在 Java 中,List 是一个接口,正如您所说: Interface is a thing where you can specify requirements for a structure
- 它就像一个需要填写的职位描述。但是你不能把工作描述从纸上剪下来,简单地用它来完成工作,即List
工作描述is abstract; cannot be instantiated
。
Instead, you need candidates to fill the position described by the job description for List
. The candidates ("concrete implementations") that meet the requirements for the job are ArrayList
, LinkedList
, Vector
, etc. Unless you initialize your List<String> ops
var with a specific candidate to do the job, you've got no one (null
) to actually do the work (there by raising a NullPointerException
.
相反,您需要候选人来填补职位描述中描述的职位List
。满足工作要求的候选人(“具体实现”)有ArrayList
、LinkedList
、Vector
等。 除非您List<String> ops
使用特定候选人初始化您的var 来完成工作,否则您没有人 ( null
) 实际完成工作(有通过提高一个NullPointerException
.
List<String> ops = new ArrayList<String>();