meme123:
Tengo esta implantación laboratorio / codificación en mi curso de estructura de datos para hacer una cola utilizando lineal Vinculado Lista pero throwrs una NullPointerException
en mi método de visualización y yo no ¿dónde está mi punto Ault
El código de clase lista Cola vinculado:
package lab10_moudhi;
/**
*
* @author Moudhi
*/
public class QueueLinkedList<E> {
private static class Node<E>{
private E data;
private Node<E> next;
public Node(E data, Node<E> next) {
this.data = data;
this.next = next;
}
public E getData() {
return data;
}
public void setData(E data) {
this.data = data;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> next) {
this.next = next;
}
@Override
public String toString() {
return data + "";
}
}
private Node<E> front=null,rear=null;
private int size=0;
public QueueLinkedList() {
}
public boolean isEmpty(){
return size==0;
}
public int size(){
return size;
}
public Node<E> getFront(){
if(isEmpty()) return null;
return front;}
public Node<E> getRear(){
if(isEmpty()) return null;
return rear;}
public void enQueue(E value){
System.out.println(value+" - Added to Queue");
Node newNode=new Node(value,null);
if(isEmpty()){
front=rear=newNode;
}
else{
rear.next=newNode;
rear=newNode;
}
size++;
}
public Node<E> deQueue(){
if(isEmpty()){
System.out.println("Queue underflow");
return null;
}
E temp=front.data;
front=front.next;
rear.next=front;
size--;
System.out.println(temp+" -Deleted from Queue");
if(size==0) front=rear=null;
return (Node<E>) temp;
}
public void display(){
Node current=front;
System.out.println("----------------------Display Queue----------------------");
if(isEmpty()){
System.out.println("Empty Queue!!");
return ;
}
do{
System.out.println(current.getData());//it throws the null pointer error here
current=current.next;
}while(current!=front);
System.out.println("--------------------------------------------------------");
}
}
El método de limpieza ::
package lab10_moudhi;
/**
*
* @author Moudhi
*/
public class Lab10_moudhi {
public static void main(String[] args) {
System.out.println("Testing a Queue using Linear Linked List:");
System.out.println("--------------------------------------------");
QueueLinkedList q1= new QueueLinkedList();
q1.enQueue(10);
q1.enQueue(20);
q1.enQueue(30);
q1.enQueue(40);
q1.display();// when i call this method it throws a null pointer exception
q1.deQueue();
q1.deQueue();
System.out.println("Front Element:"+q1.getFront());
System.out.println("Rear Element:"+q1.getRear());
q1.enQueue(50);
q1.display();
}
}
El siguiente error se lanza ::
run:
Testing a Queue using Linear Linked List:
--------------------------------------------
10 - Added to Queue
20 - Added to Queue
30 - Added to Queue
40 - Added to Queue
Exception in thread "main" java.lang.NullPointerException
----------------------Display Queue----------------------
10
20
30
40
at lab10_moudhi.QueueLinkedList.display(QueueLinkedList.java:93)
at lab10_moudhi.Lab10_moudhi.main(Lab10_moudhi.java:16)
C:\Users\Moudhi\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
puerta trasera :
Cambiar su do-while condición de bucle de modo que cuando la corriente es nula, terminar el bucle.
public void display() {
Node current = front;
System.out.println("----------------------Display Queue----------------------");
if (isEmpty()) {
System.out.println("Empty Queue!!");
return;
}
do {
System.out.println(current.getData());// it throws the null pointer error here
current = current.next;
} while (current != null); //when current becomes null end the loop
System.out.println("--------------------------------------------------------");
}
Además, el método de nodo quitar de la cola () como siguen, añadió coment Inline.
public Node<E> deQueue() {
if (isEmpty()) {
System.out.println("Queue underflow");
return null;
}
Node<E> temp = front; //Change from E to Node<E> becuase you return Node<e>, as you cannot cast from Integer to Node<E>
front = front.next;
// rear.next = front; // Also comment this line, this make rear to point to front which you have not done in your enqueue steps or either make this at both places
size--;
System.out.println(temp + " -Deleted from Queue");
if (size == 0)
front = rear = null;
return (Node<E>) temp;
}
SALIDA después de los cambios anteriores: -
Testing a Queue using Linear Linked List:
--------------------------------------------
10 - Added to Queue
20 - Added to Queue
30 - Added to Queue
40 - Added to Queue
----------------------Display Queue----------------------
10
20
30
40
--------------------------------------------------------
10 -Deleted from Queue
20 -Deleted from Queue
Front Element:30
Rear Element:40
50 - Added to Queue
----------------------Display Queue----------------------
30
40
50
--------------------------------------------------------