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
inLinkedList.remove
, withoutinicio
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– Gabe
Thanks for your help with the @Gabe question
– MagShania
Two questions: 1. Exception no longer carries messages other than those posted? 2. The stack has elements?
– Jéf Bueno
@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
– Gabe
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.– Jéf Bueno