Java: ListA.addAll(ListB) fires NullPointerException?

25,520

Solution 1

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.

Solution 2

You need to instantiate the ops list.

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

or another list type of your choosing.

Solution 3

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.

Solution 4

ops hasn't been initialized yet.

change declaration to:

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

Solution 5

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);
            }
    }
}
Share:
25,520
hhh
Author by

hhh

Updated on May 09, 2020

Comments

  • hhh
    hhh almost 4 years

    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?