简体   繁体   English

单元测试时出现 NullPointerException 错误

[英]NullPointerException error when unit testing

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.当我对我的MyStack class 的poppeek方法进行单元测试时,我遇到了与我的节点 class 的getData方法相关的NullPointerException

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 .我不知道为什么,我想知道是否有人对如何修复它有任何想法并使它不存在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.我曾尝试编辑节点的工作方式以及getData本身的工作方式,但找不到解决方案,因为无法找出问题所在。 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;
    }
}

One problem is in your push method.一个问题是你的push方法。 There, you are not assigning the new head to the member variable defined at class-level.在那里,您没有将新分配给在类级别定义的成员变量。 An updated push method could look like this:更新的push方法可能如下所示:

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() :peek中,您应该在尝试访问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.另一个NullPointerException发生在head.getData()nullsearch方法中,用于空堆栈。 Furthermore, this method does not report the correct position of an item on the stack.此外,此方法不会报告堆栈中项目的正确 position。 I won't go into details in this answer as you have already asked a separate question .我不会 go 在此答案中详细介绍,因为您已经提出了一个单独的问题


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:这是三个资源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM