How do I initialise a static List<T>?
I can't seem to understand what is going 开发者_运维问答wrong 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..... Due to this when I go on to add in the list it complains of null pointer exception..... Can anybody tell me what am i doing wrong....
List is an abstract type that is extended and implemented by various types of lists. Try the following:
public static void main ( String [] arg ) throws IOException {
vertices = new ArrayList<Vertex>();
}
List is an interface and can not be instantinated. Use ArrayList or LinkedList instead.
vertices = new ArrayList<Vertex>();
A List is an interface. You need to use a class that implements List such as ArrayList.
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
List
is no a class, but interface. As interface is not a full concrete implementation of anything it can be instantiated. You can do new only to not abstract classes. So try to instantiate ArrayList
or another implementation.
You need to use an implementation of a List, such as:
vertices = new ArrayList<Vertex>();
Eclipse complains because List can't be instantiated because it is an interface and 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
}
精彩评论