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?
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.
You need to instantiate the ops list.
public static List<String> ops = new ArrayList<String>();
or another list type of your choosing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With