2012-04-11 22 views
1

私たちの宿題については、Chairオブジェクトを取り込んで、私たちが作成したDoublyLinkedListに追加する必要があります。アルファベット順にソートする必要があります。スタイルがアルファベット順であれば、色で並べ替えます。whileループwhile compare with compareTo

私はループを通過しようとすると、私は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クラス: クラスCDoublyLinkedList { ノード最初、最後、

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); 
} 
} 

誰かが私に説明していただけますか?ありがとうございました!

EDIT:

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ごとに反復をチェックしていませんでしたので、私はエラーを得た理由でした。

+0

内部オブジェクトの1つまたは複数が最初にnullかオブジェクトまたはスタイルのように見える内部例外を取得するためにtry catchにループを入れてみてください – legrandviking

+5

何かがnullなのでエラーが発生しています。何を見つけるには、stacktraceを見て、例外が発生した行を確認してください。 – trutheality

+0

プロジェクト内で参照されているオブジェクトがインスタンス化されていないか、nullに設定されています。プログラムをデバッグし、エラーがどの行にあるかを教えてください。これは、あなた自身の問題を解決するのに役立つかもしれません。 – parion

答えて

2

あなたは、これは例外を発生させたコードの行であると言う:

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

あなたはおそらく、そのライン上のデバッガのブレークポイントを設定し、nullの値を決定するためにデバッガを使用する必要があります。しかし、ここでは、デバッガをセットアップして使用する方法の完全な説明をここで説明することは難しいです(それはあなたが学ぶべきではありません!あなたは、次の3行のコードを置き換えるために上記のコードステートメントを使用している場合

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"); 
    } 

} 

:デバッガは、私はちょうど変数が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以外の値を持つようにするために必要な手順を実行することです。

+0

良い答え。しかしちょっと残酷かもしれません... –

+0

事は、私はちょうどこれをtry-catchに入れ、例外をスローしてもwhileループのコードが動作します。私も "temp == null"をチェックしようとしましたが、何も表示されませんでした。 – prunes4u

+0

私はそれを修正しました。なんらかの理由でコードは正常に動作しますが、NullPointerExceptionがスローされます。私はちょうどスタックトレースを取り出し、私が望むように情報を出力します。 – prunes4u

2

addFirst(Chair element)をご覧ください。その方法は本当に台無しです。新しいNodeが作成され、正しいChairが含まれています。その後、prevnullに設定します。次に、nextfirstに設定します。そして、これがあなたのトラブルの原因となっています。 firstは空を指しているのでNodeです。あなたはこれで終わります:

firstあなたの新しいNodeを指しています。それはNodeを指し、それはChairを保持しません。それは再びlastを指しています。

E:あなたがあなたのリストを実装するには、少なくとも2つの異なるアプローチを持っていたし、それらをtoghether投げたよう

あなたの全体のコードが見えます。いくつかのエラーがありますが、これは宿題なので最初に修正しようとすると悪くはないと思います。

これを修正する方法がわからない場合は、こちらからお問い合わせください。

PS:私の解答を編集したり削除したりすることは申し訳ありません。私はちょっと疲れていて、古いものを修正することによって新しいエラーを引き起こし続けました。

+0

+1を取得しません。私は十分な勇気はなかった! –