空指针异常错误在单元测试时发生

huangapple go评论78阅读模式
英文:

NullPointerException error when unit testing

问题

当我对我的 MyStack 类的 poppeek 方法进行单元测试时,我遇到了与节点类的 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;
    }
}

请注意,我已经对代码进行了一些修正,特别是在 pushsearch 方法中。这应该能够帮助您解决 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&lt;E&gt; implements StackInterface&lt;E&gt;
{
public Node&lt;E&gt; 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 &amp;&amp; nodeCount == 0) {
return true;
} else {
return false;
}
}
public void push(E data) {
Node&lt;E&gt; head = new Node&lt;E&gt;(data);
nodeCount++;
}
public int search(Object o) {
int count = 0;
Node&lt;E&gt; current = new Node&lt;E&gt;(head.getData());
while (current.getData() != o) {
current.getNext();
count++;
}
return count;
}
}
public class Node&lt;E&gt; 
{
public E data;
public Node&lt;E&gt; 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&lt;E&gt; getNext() {
return next;
}
public void setNext(Node&lt;E&gt; 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&lt;E&gt; newHead = new Node&lt;&gt;(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:

huangapple
  • 本文由 发表于 2020年10月11日 17:19:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64302416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定