2016-11-15 6 views
-1

はここに私のNodeオブジェクトです:カスタムリンクリストの最後に挿入するときにNullPointerExceptionが発生するのはなぜですか?

public class Node<Any> { 

protected Any data; 
protected Node<Any> link; 

public Node() { 
    this.data=null; 
    this.link=null; 
} 

public Node(Any data, Node<Any> link) { 
    this.data=data; 
    this.link=link; 
} 

public void setData(Any data) { 
    this.data=data; 
} 

public void setLink(Node<Any> link) { 
    this.link=link; 
} 

public Any getData() { 
    return this.data; 
} 

public Node<Any> getLink() { 
    return this.link; 
} 

public String toString() { 
    return "Data: "+this.data+" | Link: "+this.link; 
} 
} 

そして、ここに私のSinglyLinkedListオブジェクトです:リンクリストに値が含まれている場合、私はこの方法を使用するたびに

public class SinglyLinkedList<Any> { 
private Node<Any> head; 
private Node<Any> tail; 
private int size; 

public SinglyLinkedList() { 
    this.head=null; 
    this.tail=null; 
    this.size=0; 
} 

//overloaded constructor for array to be added here 

public void insertAsHead(Any thing) { 
    Node<Any> tmp=new Node<Any>(); //Create new node 
    tmp.data=thing; //new_node->node.data=new_value 
    tmp.link=this.head; //new_node->node.link=head 
    this.head=tmp; //head=new_node 
    size++; 
} 

public void insertAsTail(Any thing) { 
    if(head==null) 
     insertAsHead(thing); 
    else { 
     Node<Any> tmp = new Node<Any>(); //Create new node 
     tmp.data=thing; //new_node->node.data=new_value 
     tmp.link=null; //new_node->node.link=null 
     this.tail.link=tmp; //tail->node.link=new_node 
     this.tail=tmp; //tail=new_node; 
    } 
    size++; 
} 

public void insertBefore(int i, Any thing) { 

} 

public void insertAfter(int i, Any thing) { 

} 

public void insertAt(int i, Any thing) { 

} 

public void deleteHead() { 
    size--; 
} 

public void deleteTail() { 
    size--; 
} 

public void deleteAt(int i) { 
    size--; 
} 

public Any retrieve(int i) { 
    return null; 
} 

public void set(int i, Any thing) { 

} 

public boolean isEmpty() { 
    if(this.size==0) 
     return true; 
    else 
     return false; 
} 

public int size() { 
    return this.size; 
} 

public String toString() { 
    String s="["+this.head.data; 
    Node<Any> next = this.head.link; 
    while(next!=null) { 
     s+=", "+next.data; 
     next=next.link; 
    } 
    return s+"]"; 
} 
} 

「insertAsTail(x)は」、プログラムの実行が停止して語ります行番号this.tail.link=tmp; //tail->node.link=new_nodeにNullPointerExceptionが存在することを示します。私はthis.tail.setLink(tmp)ルートに行くことを試みましたが、同じ例外が発生します。

+0

私はそれがまだ完全ではないということを忘れてしまったので、コードのいくつかの部分で空を許してください。 – ArcIX

+3

[NullPointerExceptionとは何か、それを修正する方法は?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) ) – khelwood

+0

これは '' this.tail''がヌルであることを意味します。リストが空のときになぜそれが ''ヌル ''にならないのでしょうか? – f1sh

答えて

0

本当にデバッガの使い方を学ぶ必要があります。しかし、あなたは以下を使って発行します:

this.tail.link 

ここではnullです。

0

あなたのコードは非常に理解できません。 最後に挿入するには、頭が存在するかどうかを確認するだけです。そうでない場合は、新しいノードを作成し、頭として割り当てます。

headがすでに存在する場合は、リストの末尾まで移動する必要があります。そしてノードを追加する必要があります。

私はあなたが終わるまでリストを横断するのを見ることができません。

は擬似コード:

public void addAtEnd(int new_data) 
{ 
    Node new_node = new Node(new_data); 
    if (head == null){ 
     head = new Node(new_data); 
     return; 
    } 
    new_node.next = null; 
    Node last = head; 
    while (last.next != null) 
     last = last.next; 
    last.next = new_node; 
    return; 
} 
+0

すみません。私はそれがかなり理解できないことを認識していますが、私はロジックの構造を私のデータ構造教授が教えてくれたものに近づけようとしています。私はトラバース・ザ・オール・リスト法を検討し始めました。ありがとう、お元気ですか。 – ArcIX

0

はあなた(LinkedListの)尾のように見えますが初期化されていませんでした。最初の要素が頭部の場合、tailと同じ要素を示す必要があります。

public void insertAsHead(Any thing) { 
    Node<Any> tmp=new Node<Any>(); //Create new node 
    tmp.data=thing; //new_node->node.data=new_value 
    tmp.link=this.head; //new_node->node.link=head 
    this.head=tmp; //head=new_node 
    if(this.size == 0){ 
    this.tail=this.head; 
    } 
    size++; 
} 

基本的に最初の要素を追加すると、その要素が唯一の要素なので、先頭と末尾の両方になります。

関連する問題