刚刚使用Java完成了餐厅等候名单计划,我遇到了一些问题。我使用了一个带有链表的队列,当我点击我的GUI上的显示时,它给了我这个错误。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at waitlist.LinkedList.removeFirst(LinkedList.java:74)
at waitlist.LinkedListStack.dequeue(LinkedListStack.java:10)
at waitlist.WaitListForm.btnDisplayActionPerformed(WaitListForm.java:189)
at waitlist.WaitListForm.access$100(WaitListForm.java:6)
at waitlist.WaitListForm$2.actionPerformed(WaitListForm.java:46)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
其他一切都运行良好。
这是LinkedList.java:74
public E removeFirst() {
if (isEmpty() ) {
return null;
}
else {
***E answer = head.getElement();***
head = head.getNext();
size--;
if (size == 0) {
tail = null; // list is now empty
}
return answer;
}
}
这是LinkedListStack.java:10
**public int size() {**
return list.getSize();
}
这是WaitlistForm.java:189
private void btnDisplayActionPerformed(java.awt.event.ActionEvent evt) {
String result;
int s;
int size = stack.size();
lstOutput.removeAll();
for (s = 0; s < size; s++) {
***result = (String)stack.dequeue();***
lstOutput.add(result);
}
}
这是我的代码;
LinkedList.java
public class LinkedList<E> {
private int size;
private Node<E> head;
private Node<E> tail;
// default constructor
public LinkedList() {
size = 0;
head = null;
tail = null;
}
// read-only property
public int getSize() {
return size;
}
public boolean isEmpty() {
// replaces an if/else
return (size == 0);
}
// return but not remove head of the list
public E first() {
if ( isEmpty() ) {
return null;
}
else {
return head.getElement();
}
}
public E last() {
if ( isEmpty() ) {
return null;
}
else {
return tail.getElement();
}
}
public void addFirst(E e) {
// create a new node and make it the new head of the list
head = new Node<>(e, head);
if (size == 0) {
tail = head; // special case first item in the list
}
size++;
}
public void addLast(E e) {
// create a new node and add to the tail of the list
Node<E> newest = new Node<>(e, null);
if (size == 0) {
tail = head; // special case for the first item
}
else {
tail.setNext(newest);
}
tail = newest;
size++;
}
public E removeFirst() {
if (isEmpty() ) {
return null;
}
else {
E answer = head.getElement();
head = head.getNext();
size--;
if (size == 0) {
tail = null; // list is now empty
}
return answer;
}
}
// nested class
public class Node<E> {
private E element;
private Node<E> next;
// custom constructor
public Node(E e, Node<E> n) {
element = e;
next = n;
}
// get element
public E getElement() {
return element;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> n) {
next = n;
}
} // end nested node class
} // end of LinkedList
LinkedListStack.java
public class LinkedListStack<E> implements QueueInterface<E> {
private LinkedList<E> list = new LinkedList<>(); // an empty list
public LinkedListStack() {
} // new queue relies on the initially empty list
public int size() {
return list.getSize();
}
public boolean isEmpty() {
return list.isEmpty();
}
public void enqueue(E element) {
list.addLast(element);
}
public E first() {
return list.first();
}
public E dequeue() {
return list.removeFirst();
}
}
QueueInterface.java
public interface QueueInterface<E> {
int size();
boolean isEmpty();
// adds an item to the stack
void enqueue(E e);
// return but not remove the top item on the stack
E first();
// remove item at the top of the stack
E dequeue();
}
这是我的表格(WaitlistForm.java)
int Highest = 0;
int Wait = 0;
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
Random rand = new Random();
int time = rand.nextInt(30) + 1;
int partySize = Integer.parseInt(txtSize.getText());
int total = IntStream.of(time).sum();
String name = txtName.getText();
if (partySize > Highest){
txtAvg.setText(String.valueOf(partySize));
txtTotal.setText(name);
Highest = partySize;
}
String result = " Name: " + txtName.getText() + " Party Size: " + partySize + " Wait Time: " + time + " minutes.";
stack.enqueue(result);
Wait++;
}
private void btnDisplayActionPerformed(java.awt.event.ActionEvent evt) {
String result;
int s;
int size = stack.size();
lstOutput.removeAll();
for (s = 0; s < size; s++) {
result = (String)stack.dequeue();
lstOutput.add(result);
}
}
答案 0 :(得分:0)
您收到此错误是因为LinkedList
中的基础LinkedListStack
未正确构建。最大的提示是该行上有问题的NullPointerException
表明head
的值为空,即使您的isEmpty()
消息返回false。这意味着您的列表包含head = null
和size != 0
。
您的LinkedListStack
通过调用我已在此处重新粘贴的addLast()
方法向列表添加元素:
public void addLast(E e) {
// create a new node and add to the tail of the list
Node<E> newest = new Node<>(e, null);
if (size == 0) {
tail = head; // special case for the first item
}
else {
tail.setNext(newest);
}
tail = newest;
size++;
}
尝试了解添加第一个元素时发生的情况。 head
和tail
都将为空。你输入你的特殊情况&#34;如果&#34;分支,并设置tail = head
。由于两者都已为空,因此无法执行任何操作。然后,您继续并将tail
设置为newest
,但您从未在任何地方设置head
。这意味着无论您向数据结构添加多少元素,head
始终为null。要解决此问题,您可以尝试以下方法:
public void addLast(E e) {
// create a new node and add to the tail of the list
Node<E> newest = new Node<>(e, null);
if (size == 0) { // special case for the first item
head = newest; // now head points to the new node
}
else {
tail.setNext(newest);
}
tail = newest;
size++;
}
我对您的代码进行了最小的更改,以便您可以更好地了解它之前被破坏的原因。请注意,对于第一个元素,我们现在将head
指向最新节点。
我注意到的其他一些事情:
addFirst
中有类似的错误。我想如果您了解addLast
被破坏的原因,您应该能够轻松提出修复。Stack
和Queue
的概念混为一谈。一般来说,堆栈是&#34;后进先出&#34 ;,而队列是&#34;先进先出&#34;。您已创建的LinkedListStack
课程将addLast
调用为&#34;推送&#34;元素到堆栈上,removeFirst
到&#34; pop&#34;堆栈中的元素。这意味着您的数据结构看起来很像典型的队列,而不是堆栈。