Java:ListA.addAll(ListB) 引发 NullPointerException?

发布于 2024-08-31 23:03:55 字数 990 浏览 3 评论 0原文

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);
                }
        }
}

为什么初始化时不使用new List()?

不初始化的原因是无法使用= new List()。我看不出有什么逻辑不允许这样做。它一定与数据结构等内在因素有关。

Test.java:7: java.util.List 是抽象的;无法实例化 公共静态列表ops = new List();

为什么list是一个接口?

我知道很多数据结构比如stack都实现了list。但我无法理解为什么 List 是一个接口,而为什么 Table 不是一个接口。我将列表视为一种原始结构,您可以使用它来实现其他结构。接口是您可以指定结构要求的东西。成为接口的原因是原始性还是广泛性?

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.

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?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

我要还你自由 2024-09-07 23:03:55

因为 ops 为空。 List 是一个接口这一事实并不意味着您无法初始化该字段:

public static List<String> ops = new ArrayList<String>();

List 是一个接口,因为在提供相同约定的同时有多种实现它的方法(尽管性能特征不同)。例如,ArrayList 是数组支持的,而 LinkedList 是链接列表。

Because ops is null. The fact that List is an interface does not mean you can't initialize the field:

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.

烟沫凡尘 2024-09-07 23:03:55

您需要实例化操作列表。

public static List<String> ops = new ArrayList<String>();

或您选择的其他列表类型。

You need to instantiate the ops list.

public static List<String> ops = new ArrayList<String>();

or another list type of your choosing.

微凉徒眸意 2024-09-07 23:03:55

您还没有初始化列表操作;

例如,

public static List<String> ops = new ArrayList<String>();

或者你可以这样做

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);
            }
    }
}

You have not initialized 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);
            }
    }
}
酒浓于脸红 2024-09-07 23:03:55

ops 尚未初始化。

将声明更改为:

public static List<String> ops = new ArrayList<String>();

ops hasn't been initialized yet.

change declaration to:

public static List<String> ops = new ArrayList<String>();
暖树树初阳… 2024-09-07 23:03:55

ops 从未初始化。

在执行 addAll 命令之前,您必须执行 ops = new ArrayList(); 。否则你将调用一个空对象。

不能执行 ops = new List' 的原因是 List 是一个接口,无法初始化。 ArrayList 不是抽象类型并且扩展了 List,因此在这种情况下它是合适的。

抽象类型和接口不能创建为实际对象,只能用于引用某些具体对象。具体类型必须扩展您尝试使用的抽象类型或接口。

ops is never initialized.

You have to do ops = new ArrayList<String>(); before you do the addAll command. Otherwise you are calling a null object.

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.

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.

难理解 2024-09-07 23:03:55

你要添加什么?变量 ops 仍然为 null,就像下面的 s 为 null 一样:

public static String s;

You are adding to what? Variable ops is still null, just like s is null in the following :

public static String s;
调妓 2024-09-07 23:03:55

我认为您真正的问题是:

为什么我不能实例化列表?
为什么我得到: Test.java:7: java.util.List is Abstract;无法实例化 public static List; ops = new List();

在 Java 中,List 是一个接口,正如你所说:接口是一个可以指定结构要求的东西 - 它就像需要填写的工作描述。但是你不能把工作描述从纸上剪下来,然后简单地用它来完成工作,即List工作描述是抽象的;无法实例化

相反,您需要候选人来填补 List 职位描述中描述的职位。满足作业要求的候选(“具体实现”)有 ArrayListLinkedListVector 等。除非您初始化 <代码>列表<字符串>如果您将 ops var 指定为特定候选人来完成这项工作,则没有人 (null) 实际完成这项工作(从而引发 NullPointerException)。

List<String> ops = new ArrayList<String>();

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>();

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.

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<String> ops = new ArrayList<String>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文