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
source share
7 answers

A list is an abstract type that is expanded and implemented by various types of lists. Try the following:

  public static void main ( String [] arg ) throws IOException { vertices = new ArrayList<Vertex>(); } 
+4
source

A list is an interface and cannot be created. Use an ArrayList or LinkedList instead.

 vertices = new ArrayList<Vertex>(); 
+1
source

A list is an interface. You need to use a class that implements List, such as ArrayList.

0
source

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
source

List not a class, but an interface. Since an interface is not a complete concrete implementation of something, it can be created. You can only make new, not abstract classes. So try creating an instance of ArrayList or another implementation.

0
source

You need to use the List implementation, for example:

 vertices = new ArrayList<Vertex>(); 
0
source

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
source

Source: https://habr.com/ru/post/1343632/


All Articles