Nullpointerexception when removing stack element

Asked

Viewed 332 times

1

I am trying to implement a stack structure in Java, 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 going on? (error lines are marked on code snippets)

My show:

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

}
  • the question had a lot of code and it’s hard for anyone to follow what’s going on. I tried to clean up, keeping only the parts relevant to the execution of the error (removing, for example, what had to do with the queue). I hope I helped. Also, I’m almost sure the problem is the reference to inicio.getAnterior in LinkedList.remove, without inicio has a value. Unfortunately I haven’t used Java for years and I can’t confirm or suggest a solution, but others can. If you want, [mcve] can help you create smaller and more practical examples to post here

  • Thanks for your help with the @Gabe question

  • Two questions: 1. Exception no longer carries messages other than those posted? 2. The stack has elements?

  • @jbueno The Exception and the entire code of the program are in the previous edition. From what I saw, this is the only exception message and the stack has 3 elements before the first pop

  • It’s @Gabe. This could be the trace but I think that the Exception brings with it some message. I suppose it is a NullPointerException in some of the methods chained to that line, but at this time of day (it’s 9:00 here) it’s a little difficult to read the code rationally. Especially with so much time away from Java.

1 answer

6


Consider the excerpt:

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

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

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

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

  • 1

    Thank you! Solved...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.