How to initialize a static list <T>?
I can not understand what is happening in this declaration.
public static List<Vertex> vertices; // where Vertex is a class with a default constructor public static void main ( String [] arg ) throws IOException { vertices = new List<Vertex>(); // eclipse complains } Where and how should I initialize this list ..... Because of this, when I keep adding to the list, it complains about null pointer exception ..... Can someone tell me what I'm doing wrong .. ..
+4
user506710
source share7 answers
Try:
vertices = new ArrayList<Vertex>(); List is an interface in Java, so you need to use one of its implementations.
http://download.oracle.com/javase/6/docs/api/java/util/List.html
0
Eclipse complains because List cannot be created, because it is an interface, not a concrete class. You have 2 options here -
Option1:
public static List<Vertex> vertices; // where Vertex is a class with a default constructor public static void main ( String [] arg ) throws IOException { vertices = new ArrayList<Vertex>(); // eclipse does not complain } Option2:
public static List<Vertex> vertices=new ArrayList<Vertex>(); // where Vertex is a class with a default constructor public static void main ( String [] arg ) throws IOException { v } 0