0

I'm trying to create a list structure for a simple message board usinsg lists of topics, which are themselves lists of messages, with my own implementation of binodes. However I'm getting a null pointer exception when I'm trying to add a post to my topic.

Exception in thread "main" java.lang.NullPointerException
    at TopicList.remove(TopicList.java:110)
    at TopicList.addPost(TopicList.java:80)
    at TestHarness.main(TestHarness.java:13)

My code is as follows: TestHarness.main

TopicList list = new TopicList();
list.addTop(new Message("user1", "post1"), "Topic 0");
list.addTop(new Message("user2", "post2"), "Topic 1");
list.addPost(new Message("user3", "post3"), 0);
System.out.println(list);


TopicList.addPost()

public void addPost(Message m, int id){
    Topic t;
    t = (Topic) getTopicNode(id).head();
    this.remove(getTopicNode(id)); //This is line 80 in my code
    t.addMessage(m);
    topiclist = new Node(t, topiclist);
    }


TopicList.remove()

private void remove(Node n){
    if (n.tail().equals(null)){ // this is line 110
        n.prev().nullTail();    
    }  
    else{
        n.prev().join(n.tail());
    }
}   

Thanks for helping.

4

1 回答 1

1

如果n.tail()null,以下行将抛出 NPE:

if (n.tail().equals(null)){

您需要检查:

if (n.tail() == null){ 

反而。

于 2013-11-05T12:25:46.820 回答