1

对于我们的作业,我必须接受Chair对象并将它们添加到DoublyLinkedList我们制作的东西中;它必须按字母顺序排序,如果样式按字母顺序相同,我们按颜色排序

当我尝试通过循环时,我不断得到一个NullPointerException.

public void add(Chair element){
    if(isEmpty() || first.object.style.compareTo(element.style) > 0 || (first.object.style.compareTo(element.style) == 0 && first.object.color.compareTo(element.color) >= 0){
        addFirst(element);
    }else if(first.object.style.compareTo(element.style) <= 0){
        Node temp = first;
        Node insert = new Node(); insert.object = element;
        while(temp.object.style.compareTo(element.style) <= 0) //This is where the nullPointerException occurs
            if(temp.hasNext())
                temp = temp.next;
        while(temp.object.style.compareTo(element.style) == 0 && temp.object.color.compareTo(element.color) <= 0)
            if(temp.hasNext())
                temp = temp.next;
        insert.prev = temp.prev;
        insert.next = temp;
        temp.prev.next = insert;
        temp.prev = insert;
    }
}

这是我将信息放入 DoublyLinkedList 的代码

try{
        FileReader fr = new FileReader(filename);
        Scanner sc = new Scanner(fr);
        String[] temp;

        while(sc.hasNext()){
            temp = sc.nextLine().split(" ");
            if(temp[0].equals("Bed")){}
            else if(temp[0].equals("Table")){
            //  tables.add(new Table(Integer.parseInt(temp[1]), Integer.parseInt(temp[2]), Integer.parseInt(temp[3]), temp[4]));
            }else if(temp[0].equals("Desk")){}
            else if(temp[0].equals("Chair")){
                chairs.add(new Chair(temp[1], temp[2]));
            }else if(temp[0].equals("Bookshelves")){}
            else{
                color = temp[0];
            }
        }
        while(!chairs.isEmpty())
            System.out.println(chairs.removeFirst().info());
        System.out.println();
        //while(!tables.isEmpty())
        //  System.out.println(tables.removeFirst().info());
    }catch(Exception e){e.printStackTrace();}

这是我创建的 DoublyLinkedList 类: class CDoublyLinkedList{ Node first, last;

public CDoublyLinkedList(){
    first = new Node(); last = new Node();
    first.prev = last.next = null;
    first.object = last.object = null;
    first.next = last;
    last.prev = first;
}

public boolean isEmpty(){
    return first.object == null;
}

public void addFirst(Chair element){
    Node insert = new Node();
    insert.object = element;
    insert.prev = null;
    insert.next = first;
    first.prev = insert;
    first = insert;
}

public void add(Chair element){
    if(isEmpty() || first.object.style.compareTo(element.style) > 0 || (first.object.style.compareTo(element.style) == 0 && first.object.color.compareTo(element.color) >= 0){
        addFirst(element);
    }else if(first.object.style.compareTo(element.style) <= 0){
        Node temp = first;
        Node insert = new Node(); insert.object = element;
        while(first.object.style.compareTo(element.style) <= 0)
            if(temp.hasNext())
                temp = temp.next;
        while(first.object.style.compareTo(element.style) == 0 && first.object.color.compareTo(element.color) <= 0)
            if(temp.hasNext())
                temp = temp.next;
        insert.prev = temp.prev;
        insert.next = temp;
        temp.prev.next = insert;
        temp.prev = insert;
    }
}

public Chair removeFirst(){
    Chair tobedeleted = first.object;
    Node temp = first.next;
    first = temp;
    first.prev = null;
    return tobedeleted;
}

private class Node{
    Node next, prev;
    Chair object;
    public boolean hasNext(){
        return next != null;
    }
}

}

主席班:

class Chair extends Furniture{
public String style, color;
public Chair(String s, String c){
    style = s; color = c;
}
public String toString(){
    return color;
}
public String getType(){
    return "Chair";
}
public String info(){
    return (color+", "+style);
}
 }

有人可以向我解释为什么我不断收到此错误吗?谢谢!

编辑:

while(temp.object.style.compareTo(element.style) <= 0) //This is where the nullPointerException occurs

chairs.add(new Chair(temp[1], temp[2]));

java.lang.NullPointerException
at CDoublyLinkedList.add(Furnish2SS.java:119)
at Furnish2SS.main(Furnish2SS.java:23)
java.lang.NullPointerException
at CDoublyLinkedList.add(Furnish2SS.java:119)
at Furnish2SS.main(Furnish2SS.java:23)

EDIT2:解决了!

我将while循环更改为:

while(temp.object != null && element != null && (temp.object.compareTo(element) == 0 || temp.object.compareTo(element) == -1))

我收到错误的原因是因为我没有检查null每次迭代。

4

2 回答 2

2

看看addFirst(Chair element)。那个方法真的太坑了。它创建一个Node包含正确Chair. 然后将其设置prevnull。然后它设置nextfirst。这就是造成你所有麻烦的原因。因为first指向一个空Node。你最终得到这个:

first指向你的新Node. 那 一个 指向Node不 持有的Chair。那又是一指last

e:

您的整个代码看起来至少有两种不同的方法来实现您的列表并将它们放在一起。还有一些错误,但是由于这是家庭作业,我想如果您先尝试修复它,那还不错。

如果您不知道如何纠正,请在此处询问。

PS:很抱歉所有的编辑和(取消)删除我的答案(如果你注意到的话)。我有点累了,不断地通过修复旧错误来制造新的错误,直到我终于弄清楚这一切的真正原因是什么。

于 2012-04-11T22:13:22.730 回答
2

你说这是导致异常的代码行:

while(temp.object.style.compareTo(element.style) <= 0)

您可能应该在该行上设置一个调试器断点并使用调试器来确定哪些值为空。但是我很难在这里解释如何设置和使用调试器的完整说明(这并不意味着你不应该学习!你应该学习。有很多教程。谷歌它。)所以而不是写一个关于调试器,我将发布代码来告诉您哪个变量为空:

if (temp == null) {
    System.out.println("temp is null");
} else if (temp.object == null) {
    System.out.println("temp.object is null");
} else if (temp.object.style == null) {
    System.out.println("temp.object.style is null");
} 

if (element == null) {
    System.out.println("element is null");
} else if (element.style == null) {
    System.out.println("element.style is null");
}


while(temp.object.style.compareTo(element.style) <= 0) //This is where the nullPointerException occurs
{
    if(temp.hasNext())
        temp = temp.next;

    if (temp == null) {
        System.out.println("loop: temp is null");
    } else if (temp.object == null) {
        System.out.println("loop: temp.object is null");
    } else if (temp.object.style == null) {
        System.out.println("loop: temp.object.style is null");
    } 

    if (element == null) {
        System.out.println("loop: element is null");
    } else if (element.style == null) {
        System.out.println("loop: element.style is null");
    }

}

如果您使用上面的代码语句来替换这三行代码:

    while(temp.object.style.compareTo(element.style) <= 0) //This is where the nullPointerException occurs
        if(temp.hasNext())
            temp = temp.next;

您将根据打印的语句知道哪个变量为空。希望你能从那里拿走它。(修复 NullPointerException 的常用方法是采取必要的步骤,以确保在程序到达 NullPointerException 行时,有问题的 null 变量实际上具有有效的非 null 值)。

于 2012-04-11T20:28:30.800 回答