-1

*があり、特定の対象アイテムを削除して返す remove メソッドを取得しようとしています。機能させるためにさまざまな方法を試しましたが、NPEが発生し続けます。

ここに私の最初の remove() があります:

コードをコンパイルすることができた 2 番目の remove() を次に示します。

ここに私のLinearNodeがあります:

学生クラス:

4

2 に答える 2

1

削除するのはかなり簡単で、一般的な考え方はすでにわかっています。

public Student remove(Student items) {
    LinearNode  previous = null,
                current = head;

    // iterate over all the nodes starting at the head, maintaining a reference to the previous node as you go          
    while (current != null && current.items.compareTo(items) != 0) {
        previous = current;
        current = current.next;
    }

    // At this point you have either a) found the Node with matching items or b) not found it
    if (current == null) {
        // not found in the list
        return null;
    }

    // At this point you know where the Node is, and you have a reference previous node as well 
    // so it's easy to reattach the linked list to remove the node
    if (previous == null) {
        // The head node was the match if previous is not set, so make sure to update the head Node accordingly
        head = current.next;
    }
    else {
        previous.next = current.next
    }

    return current.items;
}
于 2013-11-07T14:52:53.900 に答える
0

問題の原因となっている行が見つかるまで、疑わしいコードに対して try catch を使用します。

これにより、正しい場所を見ているかどうかがわかります。

例えば:

  while (current != null) {
     try{//1st level try
        if(current.items.compareTo(items) == 0) {
          try{ //2nd level try 
            if(previous == null) {
                head = head.next;
                return items;
            }
            else {
                previous.next = current.next;
                return items;
            }
          }catch(NullPointerException e ){
             StackTraceElement t = e.getStackTrace()[0];  
             System.out.println("catch lvl 2 at line: " + t.getLineNumber());
          }
        }
        else {
            previous = current;
            current = current.next;
        }
      }catch(NullPointerException e){
         StackTraceElement t = e.getStackTrace()[0]; 
         System.out.println("catch lvl 1 at line: " + t.getLineNumber());
      }
    }

編集:

この「try catch」を配置して、すべてのメイン関数をラップすることができます。

try{
  ...
}catch(NullPointerException e ){
  int i=0
  for( StackTraceElement t : e.getStackTrace()){
     System.out.println("stack[" + i + "]: " + t.getLineNumber());
     i++;
  }
}

EDIT2

public Student remove(Integer studentId)
{
    LinearNode previous = null;
    LinearNode current = head;

    while (current != null) {
        //if everything is OK you can remove the 2 ifs 
        if(current.items == null){
           //something is really wrong on insert
        }
        else if(current.items.getId() == null){
           //something is really wrong on insert
        }
        else if( studentId.compareTo(curent.items.getId()) == 0) {

            //return value is curent.items;

            if(previous == null) {
                head = head.next;
                return curent.items;
            }
            else {
                previous.next = current.next;
                return curent.items;
            }
        }
        else {
            previous = current;
            current = current.next;
        }
    }
    //not found!
    return null;
}
于 2013-11-06T11:35:32.957 に答える