NullPointerException when removing element from stack

1

I'm trying to implement a Java stack structure, but when I try to remove an element I get the following error:

> Exception in thread "main" java.lang.NullPointerException
    at br.com.magnoliamedeiros.pilhafila.LinkedList.remove(LinkedList.java:17)
    at br.com.magnoliamedeiros.pilhafila.Pilha.pop(Pilha.java:13)
    at br.com.magnoliamedeiros.pilhafila.Program.main(Program.java:22)

Any idea what might be happening? (error lines are marked in code snippets)

My program:

//...
Pilha<String> pilha = new Pilha<String>();          
//...
pilha.push("Márcia");
pilha.pop(); // LINHA #22

Class Pilha :

public class Pilha<T> extends LinkedList<T>{        
    public void push(T valor){
        add(valor);
    }

    public T pop() throws Exception{
        if(isEmpty()){
            throw new Exception("Pilha vazia!");
        }
        return this.remove(0); // LINHA #13
    }
}

Class Node :

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;
    }
}

Class LinkedList :

public class LinkedList<T> implements java.util.List<T>{
    Node<T> inicio;
    Node<T> fim;
    int size = 0;

    @Override
    public T remove(int index) {
        if(index==0){
                inicio.getAnterior().setProximo(inicio.getProximo()); // LINHA #17
                inicio.getProximo().setAnterior(inicio.getAnterior());
                inicio.setAnterior(null);
                inicio.setProximo(null);
                return inicio.getValor();
        }else{
            int i = 1;
            while(i!=this.size()){
                i++;
                inicio.setProximo(inicio.getProximo());

                if(i == index){
                    inicio.getAnterior().setProximo(inicio.getProximo());
                    inicio.getProximo().setAnterior(inicio.getAnterior());
                    inicio.setAnterior(null);
                    inicio.setProximo(null);
                    return inicio.getValor();
                }
            }
        }
        return null;
    }


    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;
        }
        size++;
        return true;
    }

    @Override
    public int size() {
        return size;
    }


    @Override
    public boolean isEmpty() {
        return size==0;
    }

}
    
asked by anonymous 12.12.2016 / 00:26

1 answer

6

Consider the excerpt:

if(index==0){
                inicio.getAnterior().setProximo(inicio.getProximo()); // LINHA #17

In this case, NullPointerExeption (NPE) occurred because if index is zero, then there is no object before and inicio.getAnterior() will return null .

The rules for managing a linked list include never accessing the previous element to the first, or the later element to the last one. It is also important to consider that if an element is the only element in the list, it is first and last at the same time.

In this case, removing the first element is as simple as making the inicio of the list point to the next element. If the list is not empty, of course. If the list contains only one element, the beginning will point to null , so you have to consider this in the code and never assume there will be any value in the variable.

    
13.12.2016 / 02:41