链接列表出错(java)

时间:2014-07-24 10:51:30

标签: java nullpointerexception linked-list

我创建了自己的链接列表,但是当我尝试运行它时出现错误:

Exception in thread "main" java.lang.NullPointerException
at List.add(List.java:8)  //if(t.val ==null)
at main.main(main.java:38) //linput.add(inputLine.split(" ")[i]);

这是我的List类:

class List{
    String val;
    List next=null; 
    private List t; 

    public void add(String word){   
        if(t.val ==null)
            t.val=word;
        else while(!t.next.val.equals(null))
        {
            t=t.next;
            if(t.next.val.equals(null))
            {
                t.next.val=word;
                break;
            }
        }

    }

    public int get(String word)
    {   
        int i=0;
        if(t.val.equals(word))
            i=0;
        else while(!t.next.val.equals(word))
        {
            t=t.next;
            i++;
            if(t.next.val.equals(word))
            {
                i++;
            }
        }

        return i;
    }

    public String indexOf(int i)
    {   
        int counter=0;
        while(counter<i)
        {
            t=t.next;
            counter++;
        }
        return t.val;
    }

}

这是我的主要功能:

static public void main(String[] args)  
{   
    List linput = new List();
    String inputLine = "Hey look at me.";
    for(int i = 0 ; i < inputLine.split(" ").length ; i++)
    {
        linput.add(inputLine.split(" ")[i]);

    }
    System.out.println(linput.indexOf(0)+" "+linput.indexOf(1)+" "+linput.indexOf(2));
}

我初始化了t,但下次有这样的错误:

private List t =new List();

Exception in thread "main" java.lang.StackOverflowError
    at List.<init>(List.java:5)
    at List.<init>(List.java:5)
    at List.<init>(List.java:5)

对不起。我无法提供完整的代码,因为我的其余代码运行良好(从txt等读取....)。

3 个答案:

答案 0 :(得分:2)

错误似乎与变量't'有关(即私有List t)。 你初始化这个变量了吗? if(t.val == null)似乎正在扼杀这一点,因为此时t为null(未初始化) 您应该为此变量分配对象(使用new)。

你能分享List的构造函数的完整代码吗?

答案 1 :(得分:1)

假设您要实现一个简单的转发列表,而不是使用Java LinkedList class,您需要:

  • 将列表的实现更改为列表中的引用节点
  • 处理单词列表中链接节点的遍历

以下是一个例子:

WordList类

package com.example.words;
class WordList {

    private WordNode head = null;
    private int listSize = 0;

    public void add(String word) {
        // TODO add check for duplicate word
        if (head == null) {
            head = new WordNode();
            head.setValue(word);
            listSize++;
        } else {
            WordNode current = head;
            while (current.getNext() != null) {
                current = current.getNext();
            }
            WordNode newNode = new WordNode();
            newNode.setValue(word);
            current.setNext(newNode);
            listSize++;
        }
    }

    public int getWordIndex(String word) {
        WordNode current = head;
        int index = 0;
        boolean found = false;
        while (!found && current != null) {
            found = current.getValue().equalsIgnoreCase(word);
            if (!found) {
                index++;
                current = current.getNext();
            }
        }
        if (found) {
            return index;
        } else {
            return -1;
        }
    }

    public String indexOf(int i) {
        int index = 0;
        WordNode current = head;
        if (i <= listSize) {
            while (index < i) {
                current = current.getNext();
                index++;
            }
            return current.getValue();
        } else {
            return null;
        }
    }
    public int size() {
        return listSize;
    }
}

WordNode类

package com.example.words;

public class WordNode {
    private String value;
    private WordNode next = null;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public WordNode getNext() {
        return next;
    }

    public void setNext(WordNode link) {
        next = link;
    }

}

测试驱动程序

package com.example.words;

public class Test {

    public static void main(String[] args) {
        //TODO handle punctuation
        WordList myList = new WordList();
        String inputLine = "Hey look at me.";

        String[] pieces = inputLine.split(" ");


        for (int i=0; i < pieces.length; i++) {
            myList.add(pieces[i]);
        }

        for (int i=0; i < pieces.length; i++) {
            String value = myList.indexOf(i);
            if (value.equalsIgnoreCase(pieces[i])) {
                System.out.println("Following node is wrong:");
            }
            System.out.println ("node " + i + ". = " + value);

        }
    }

}

答案 2 :(得分:0)

您尝试将t创建为其自己的类的成员变量,如下所示:

class List {
    [...]
    private List t = new List();
    [...]
}

这不起作用,因为List的构造函数将无限期地调用。

尝试使用t的懒惰实例化。用getter取代t的所有访问权限:

private List getT() {
    if (this.t == null) {
        this.t = new List();
    }
    return t;
}