views:

186

answers:

7

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?

+4  A: 

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.

Matthew Flaschen
Test.java:7: java.util.List is abstract; cannot be instantiated public static List<String> ops = new List<String>(); <--- but I cannot understand this.
HH
List is an interface, so it has no constructor.
Matthew Flaschen
List is an interface, but if you look at the code, it is creating an ArrayList which implements the List interface
Rob Goodwin
What does the line then really mean: "List<String> hello"? Interface called hello that -- operates with String?
HH
HH, the name of the interface is `List`. It's specialized with `String`, so `List<String>`. `hello` is a reference variable of type `List<String>`. But you really need to find a good Java tutorial.
Matthew Flaschen
Yes, perhaps you should start out with a non-generic collection
Rob Goodwin
"specialized with String" and "a reference variable"?
HH
+1  A: 

You need to instantiate the ops list.

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

or another list type of your choosing.

Rob Goodwin
+1  A: 

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.

Thomas Sidoti
+1  A: 

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);
            }
    }
}
lucasweb
+1  A: 

ops hasn't been initialized yet.

change declaration to:

public static List<String> ops = new ArrayList<String>();
ViNDi
+2  A: 

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

public static String s;
fastcodejava
A: 

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