java - 我收到两个 NullPointerExceptions,我不确定如何解决

标签 java nullpointerexception linked-list doubly-linked-list

我正在制作一个包含 vector 的双向链接循环列表,但我不知道如何修复我遇到的两个 NullPointerExceptions。它们都与我的 add 方法一起使用,一个 add 方法在列表末尾插入一个对象,另一个在给定索引处插入对象。这是我的有问题的代码。

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Vector;

public class DList<T> implements ADTListInterface<T> {
private int size;
private int BUCKET;
private DListNode head;
private DListNode last;

public DList() {
    head = new DListNode(null);
    head.next = last;
    head.prev = head;
    BUCKET = 1;
}

public DList(int bucket){
    if (bucket <= 0)
        throw new IllegalArgumentException("Cannot have a bucket lower than 1");

    this.BUCKET = bucket;
    head = new DListNode(null);
    head.next = last;
    head.prev = head;
}

private class DListNode {
    private Vector<T> item;   //Line 29
    private DListNode next;   //Line 30
    private DListNode prev;

    public DListNode(T o) {
        this(o, null, null);
    }

    public DListNode(T o, DListNode next, DListNode prev) {
        item = new Vector<T>(BUCKET);

        item.add(o);
        this.next = next;
        this.prev = prev;
    }

    public void add(T item) {
        if (this.item.size() == this.item.capacity()) {
            DListNode newNode = new DListNode(item, this.next, this);
            this.next = newNode;
        }

        else {
            insertAfter(item);
        }
    }

    public void add(T item, int idx) {
        if (this.item.size() == this.item.capacity()) {
            DListNode newNode = new DListNode(this.item.elementAt(BUCKET), this.next, this);
            this.next = newNode;
        }
        else {
            this.item.add(idx, item);
        }
    }

    public boolean remove(T o){
        if (item.indexOf(o) != -1){
            item.remove(o);
            if (item.size() == 0){
                this.prev.next = this.next;
                this.next.prev = this.prev;
            }
            size--;
            return true;
        }
        return false;
    }

    public void insertAfter(T item) {
        next = new DListNode(item, next, prev);
        next.next.prev = next;
    }

    public DListNode nth(int index) {
        if (index == 1)
            return this;
        else if (index < 0 || next == null) {
            throw new java.util.NoSuchElementException("No Such Element");
        } else
            return next.nth(index - 1);
    }
}

@Override
public void add(T o) {

    DListNode last = head;
    while (last.item.size() >= BUCKET && last.next != head) {
        last = last.next;
    }

    if (last.item.size() == BUCKET) { //Line 102
        DListNode n = new DListNode(o, head, last);
        head.prev.next = n;
        head.prev = n;
        size++;
    } else {
        last.item.add(o);
    }

}

    @Override
public void add(T o, int index) {
    DListNode temp = head, testNext;
    int count = 0;

    while (count+1 <= index) {
        count += temp.item.size();
        temp = temp.next;
        if (temp == head) {
            throw new IndexOutOfBoundsException("Out Of Bounds!");
        }
    }
    testNext = temp.next;   //Line 126
    temp.add(o, index);
    if (testNext != temp.next)
        size++;
}

我得到的两个错误是:

  1. 线程“main”中的异常 java.lang.NullPointerException 在 DList$DListNode.access$0(DList.java:29) 在 DList.add(DList.java:102) 在 DListNodeDriver.main(DListNodeDriver.java:7)

  2. 线程“main”中的异常 java.lang.NullPointerException 在 DList$DListNode.access$1(DList.java:30) 在 DList.add(DList.java:126) 在 DListNodeDriver.main(DListNodeDriver.java:6)

    谢谢您,我们将不胜感激。

主要方法

import java.util.Iterator;

public class DListNodeDriver {
    public static void main(String[] args) {
        DList<String> students = new DList<String>();
        students.add("Other", 1);
        students.add("New", 3);
        Iterator<String> t = students.iterator();
        while (t.hasNext()) {
            String temp = t.next();
            StdOut.println("Student: " + temp);
        }

    }
}

import java.util.Iterator;

public class DListNodeDriver {
    public static void main(String[] args) {
        DList<String> students = new DList<String>();
        students.add("Other");
        students.add("New");
        Iterator<String> t = students.iterator();
        while (t.hasNext()) {
            String temp = t.next();
            StdOut.println("Student: " + temp);
        }

    }
}

最佳答案

您的 temp 为空,原因很明显。通过它:

  1. 无参数构造函数 -> head.nextnull
  2. students.add("other",1); -> temp = temp.next 在循环中,因此 null

类似的问题发生在students.add("other")中。

我会根据你的实现做这样的事情:

@Override
public void add(T o, int index) {
    DListNode temp = head
    int count = 0;

    // < instead of <= because if index = size, your temp will be null otherwise.
    // if index = 1 , you'll want to add after head. Watch out for index > size! 
    // It is an IndexOutOfBounds, index = size => add behind last element.
    while (count+1 < index) { 
        count++;
        if( temp.next == null ) { /*Handle that case */ }
        else {temp = temp.next;}
        //if (temp == head) {
        //    throw new IndexOutOfBoundsException("Out Of Bounds!");
        //}
    }
    // temp should now be the element just before the index at which to insert.
    // I'd do a temp.insertAfter( o );

    /* DO YOUR ADDING HERE*/
}

关于java - 我收到两个 NullPointerExceptions,我不确定如何解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12794363/

相关文章:

java - 使用 TextArea.append() 时出现 NullPointerException

java - 无法创建附加方法来协助替换 LinkedList 中的子字符串

c - 从链表中删除最后一个元素

java - 方法是否需要同步?

java - GWT 元素间通信

java - 自定义警报对话框中的访问按钮

c++ - 链表 - 当我使用 -> 打印函数时程序中断

JavaFX 客户端和 JDBC 服务器 : Are JavaFX Bean-style objects required on the server side?

java - 不可变或不可修改集合的目的

java aes javax.crypto.BadPaddingException : Given final block not properly padded