英文:
NullPointerException error when unit testing
问题
当我对我的 MyStack
类的 pop
和 peek
方法进行单元测试时,我遇到了与节点类的 getData
方法相关的 NullPointerException
。
我无法确定原因,想知道是否有人对如何修复以及如何避免出现 NullPointerException
有任何想法。我尝试过编辑节点的工作方式以及 getData
本身的工作方式,但找不到解决方案,因此无法找出问题所在。非常感谢任何帮助。
import java.io.*;
import java.util.*;
public class MyStack<E> implements StackInterface<E>
{
public Node<E> head;
public int nodeCount = 0;
public static void main(String args[]) {
}
public E peek() {
return head.getData();
}
public E pop() {
if (nodeCount == 0) {
throw new EmptyStackException();
}
E item = head.getData();
head = head.getNext();
nodeCount--;
return item;
}
public boolean empty() {
if (head == null && nodeCount == 0) {
return true;
} else {
return false;
}
}
public void push(E data) {
Node<E> newNode = new Node<E>(data);
newNode.setNext(head);
head = newNode;
nodeCount++;
}
public int search(Object o) {
int count = 0;
Node<E> current = head;
while (current != null && !current.getData().equals(o)) {
current = current.getNext();
count++;
}
return (current != null) ? count : -1;
}
}
public class Node<E>
{
public E data;
public Node<E> next;
public Node(E data)
{
this.data = data;
this.next = null;
}
public E getData() {
return this.data;
}
public void setData(E data) {
this.data = data;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> next) {
this.next = next;
}
}
请注意,我已经对代码进行了一些修正,特别是在 push
和 search
方法中。这应该能够帮助您解决 NullPointerException
的问题。
英文:
When I unit test my pop
and peek
methods for my MyStack
class, I encounter a NullPointerException
relating to the getData
method of my node class.
I cannot tell why and I am wondering if anyone has any ideas on how to fix it and make it so that there is not a NullPointerException
. I have tried editing how the node works and how getData
itself works but cannot find a solution and since cannot figure out the problem. Any help would be very much appreciated
import java.io.*;
import java.util.*;
public class MyStack<E> implements StackInterface<E>
{
public Node<E> head;
public int nodeCount = 0;
public static void main(String args[]) {
}
public E peek() {
return head.getData();
}
public E pop() {
if (nodeCount == 0) {
throw new EmptyStackException();
}
E item = head.getData();
head = head.getNext();
nodeCount--;
return item;
}
public boolean empty() {
if (head == null && nodeCount == 0) {
return true;
} else {
return false;
}
}
public void push(E data) {
Node<E> head = new Node<E>(data);
nodeCount++;
}
public int search(Object o) {
int count = 0;
Node<E> current = new Node<E>(head.getData());
while (current.getData() != o) {
current.getNext();
count++;
}
return count;
}
}
public class Node<E>
{
public E data;
public Node<E> next;
// getters and setters
public Node(E data)
{
this.data = data;
this.next = null;
}
public E getData() {
return this.data;
}
public void setData(E data) {
this.data = data;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> next) {
this.next = next;
}
}
答案1
得分: 1
问题出在你的 push
方法中。在那里,你没有将新的 head 分配给在类级别定义的成员变量。更新后的 push
方法可能如下所示:
public void push(E data) {
Node<E> newHead = new Node<>(data);
newHead.setNext(head);
head = newHead;
nodeCount++;
}
在 peek
方法中,在尝试访问 getData()
之前,你应该检查堆栈是否为空:
public E peek() {
if (empty()) {
throw new EmptyStackException();
}
return head.getData();
}
另一个 NullPointerException
发生在 search
方法中,对于空堆栈,head.getData()
为 null
。此外,该方法不报告堆栈上项的正确位置。我不会在这个答案中详细介绍,因为你已经提出了一个单独的问题。
我强烈鼓励你研究如何使用调试器逐行执行代码。通过这种方式,你可以逐行执行程序并查看它与你的期望有何不同。调试是作为程序员的基本技能。以下是三个资源:
英文:
One problem is in your push
method. There, you are not assigning the new head to the member variable defined at class-level. An updated push
method could look like this:
public void push(E data) {
Node<E> newHead = new Node<>(data);
newHead.setNext(head);
head = newHead;
nodeCount++;
}
In peek
you should check if the stack is empty before trying to access getData()
:
public E peek() {
if (empty()) {
throw new EmptyStackException();
}
return head.getData();
}
Another NullPointerException
happens in the search
method where head.getData()
is null
for an empty stack. Furthermore, this method does not report the correct position of an item on the stack. I won't go into details in this answer as you have already asked a separate question.
I highly encourage to look into how to use a debugger to step through your code. Thereby, you can execute your program line by line and see where it is deviating from what you expect. Debugging is an essential skill as a programmer. Here are three resources:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论