I'm having trouble implementing a LinkedList
for the Data Structure discipline. The code is giving this error:
Exception in thread "main"
java.lang.NullPointerException
, and I do not quite understand why. Here is the code:
Main class Program
package br.com.magnoliamedeiros.linkedlist;
import java.util.Iterator;
public class Program {
public static void main(String[] args){
LinkedList<String> minhaLista = new LinkedList<String>();
minhaLista.add("Magnolia");
minhaLista.add("Maria");
Iterator<String> it = minhaLista.iterator();
while(it.hasNext()){
String s = it.next();
System.out.println(s);
}
}
}
Node class
package br.com.magnoliamedeiros.linkedlist;
public class Node<T> {
Node<T> proximo;
Node<T> anterior;
T valor;
public void setProximo(Node<T> proximo){
this.proximo = proximo;
}
public Node<T> getProximo(){
return proximo;
}
public void setAnterior(Node<T> anterior){
this.anterior = anterior;
}
public Node<T> getAnterior(){
return anterior;
}
public void setValor(T valor){
this.valor = valor;
}
public T getValor(){
return valor;
}
}
LinkdekList class
package br.com.magnoliamedeiros.linkedlist;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class LinkedList<T> implements java.util.List<T> {
private Node<T> inicio;
private Node<T> fim;
@Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean contains(Object o) {
// TODO Auto-generated method stub
return false;
}
@Override
public Iterator<T> iterator() {
// TODO Auto-generated method stub
return null;
}
@Override
public Object[] toArray() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean add(T e) {
Node<T> newNode = new Node<T>();
newNode.setValor(e);
if(inicio == null){
inicio = newNode;
}
if(fim == null){
fim = newNode;
}else{
fim.setProximo(newNode);
newNode.setAnterior(fim);
fim = newNode;
}
return true;
//return false;
}
@Override
public boolean remove(Object o) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean addAll(Collection<? extends T> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean addAll(int index, Collection<? extends T> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean retainAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public void clear() {
// TODO Auto-generated method stub
}
@Override
public T get(int index) {
// TODO Auto-generated method stub
return null;
}
@Override
public T set(int index, T element) {
// TODO Auto-generated method stub
return null;
}
@Override
public void add(int index, T element) {
// TODO Auto-generated method stub
}
@Override
public T remove(int index) {
// TODO Auto-generated method stub
return null;
}
@Override
public int indexOf(Object o) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int lastIndexOf(Object o) {
// TODO Auto-generated method stub
return 0;
}
@Override
public ListIterator<T> listIterator() {
// TODO Auto-generated method stub
return null;
}
@Override
public ListIterator<T> listIterator(int index) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<T> subList(int fromIndex, int toIndex) {
// TODO Auto-generated method stub
return null;
}
@Override
public <string> string[] toArray(string[] a) {
// TODO Auto-generated method stub
return null;
}
}
LinkedListIterator class
package br.com.magnoliamedeiros.linkedlist;
public class LinkedListIterator<T> implements java.util.Iterator<T> {
private Node<T> proximoNode = null;
LinkedListIterator(Node<T> inicio){
proximoNode = inicio;
}
@Override
public boolean hasNext() {
return proximoNode != null;
}
@Override
public T next() {
T valor = proximoNode.getValor();
proximoNode = proximoNode.getProximo();
return valor;
}
}