Java: ListA.addAll (ListB) throws NullPointerException?

The part of the error is capitalized in the code, it is also included in foreaching. Due to the abstract list, it cannot be initialized, the declaration is in a static field. Lists are of 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 a new List () in initialization?

The reason for not initializing was the impossibility of use = new List<String>()

. I see no logic to prevent this. It must have something to do with intrinsic factors like data structures or whatever.

Test.java:7: java.util.List is abstract; cannot be instantiated public static List<String> ops = new List<String>();

Why is a list an interface?

I know that a lot of data structures, like the stack, implement a list. But I can't figure out why List is an interface and why not Table, for example. I see the list as a primitive structure with which you can implement other structures. The interface is where you can specify the structure requirements. Is primitivenness or extensivens a reason to be an interface?

+2


a source to share


7 replies


Because ops is null. The fact that List is an interface doesn't mean that you can't initialize a field:

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

      



List

is an interface because there are several ways to implement it while simultaneously providing the same contract (albeit with different performance characteristics). For example, ArrayList

maintains an array, while LinkedList

a linked list.

+12


a source


You need to create an instance of the options list.

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

      



or another type of list of your choice.

+4


a source


ops is never initialized.

You must do ops = new ArrayList<String>();

before you run the addAll command. Otherwise, you are calling a null object.

The reason you cannot execute ops = new List<String>

'is because List is an interface and cannot be initialized. ArrayList is not an abstract type and extends a list, so it fits in this context.

Abstract types and interfaces cannot be instantiated as an actual object and can only be used to refer to a specific object. The concrete type must extend the abstract type or interface that you are trying to use.

+1


a source


You have not initialized the list option,

eg.

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

      

+1


a source


ops hasn't been initialized yet.

change your ad to:

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

      

+1


a source


Are you adding to what? The variable ops

is still null, just as s

it is null in the following:

public static String s;

      

0


a source


I think your real question is:

Why can't I instantiate the 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 that, as you say Interface is a thing where you can specify requirements for a structure

, is like a job description to be filled out. But you cannot copy a job description from paper and just use it to complete the job, i.e. Description of the task List

is abstract; cannot be instantiated

.

Instead, you need candidates to fill the position described in the job description for List

. Candidates ( "concrete realization") that meet the requirements of the job, ArrayList

, LinkedList

, Vector

, etc. Unless you initialize your List<String> ops

var with a specific candidate to do the job, you have not got any ( null

) to actually get the job done (there, raising NullPointerException

.

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

      

0


a source







All Articles