-1

As I am trying to make an array of Linked Lists, I keep getting a NullPointerException. The linked list works on its own until I make an array of it.

MyLinkedList<String>[] list = new MyLinkedList[1];

list[0].addFirst("Milo");

System.out.println(list[0].get(0));

I keep getting this.

Exception in thread "main" java.lang.NullPointerException at Test.main(Test.java:10)

Biffen
  • 6,249
  • 6
  • 28
  • 36
Branko Andrews
  • 29
  • 1
  • 1
  • 6

1 Answers1

1

You need to initialize each linked list inside the array, in addition to initializing the array itself, e.g.

MyLinkedList<String>[] list = new MyLinkedList[1];
list[0] = new MyLinkedList();
list[0].addFirst("Milo");
System.out.println(list[0].get(0));
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360