Implementation of a LinkedList

0

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;
    }
}
    
asked by anonymous 10.12.2016 / 12:53

1 answer

0

Your method that returns Iterator is not implemented. It looks like this in the code you submitted:

@Override
public Iterator<T> iterator() {
  // TODO Auto-generated method stub
  return null;
}

So it's up to implementation for your main method to work.

By the way, it's interesting that you put the integer% of% of the error, as in this case:

Exception in thread "main" java.lang.NullPointerException
    at padrao.Teste.main(Teste.java:19)
    
10.12.2016 / 13:14