Java.lang.nullPointerException в моем пользовательском классеJAVA

Программисты JAVA общаются здесь
Anonymous
Java.lang.nullPointerException в моем пользовательском классе

Сообщение Anonymous »

Я хочу внедрить очередь, основанную на простом классе связанных списков, без использования java.util < /code>. < /p>

Когда я вызываю метод приложения в классе списка через метод Enque взять?public class Node {
private int value;
private Node next;

public Node(int val) {
value = val;
}

public Node(int val, Node next) {
value = val;
this.next=next;
}

public Node(Node next) {
this.next=next;
}

public int getValue() {
return value;
}

public Node getNext() {
return next;
}

public void setNext(Node next) {
this.next = next;
}

public void displayNode() {
System.out.print(" "+value+" ");
}
}
< /code>

Мой интерфейс < /p>

public interface MyQueue {
void enqueue(int oVal);
int dequeue();
}
< /code>

Список < /p>

public class List {
private Node first;
private Node last;
private int counter;

public List() {
first = null;
last = null;
}

public boolean isEmpty() {
return first==null;
}

public void addEnd(int val) {
Node n1 = new Node(val);
if( isEmpty() ) {
first = n1;
} else {
last.setNext(n1);
last = n1;
}
}

public int deleteStart() {
int temp = first.getValue();
if(first.getNext() == null){
last = null;
first = first.getNext();
}
return temp;
}

public void displayList() {
Node current = first;
while(current != null) {
current.displayNode();
current = current.getNext();
}
System.out.println("");
}

public int size() {
return counter;
}
}
< /code>

очередь < /p>

public class Queue implements MyQueue {
private List listQ;

public Queue() {
listQ = new List();
}

public boolean isEmpty() {
return listQ.isEmpty();
}

public void enqueue(int oVal) {
listQ.addEnd(oVal);
}

public int dequeue() {
return listQ.deleteStart();
}

public void displayQueue() {
System.out.print("Queue ");
listQ.displayQueue();
}
}

public class App {
public static void main(String[] args) {
Queue q1 = new Queue();
System.out.println("Two insertions");
q1.enqueue(4);
q1.enqueue(64);
q1.displayQueue();
System.out.println("Insert at the end : ");
q1.enqueue(23);
q1.displayQueue();
System.out.println("Delete an element at the begining of the queue");
q1.dequeue();
q1.displayQueue();
}
}


Подробнее здесь: https://stackoverflow.com/questions/298 ... stom-class

Вернуться в «JAVA»