0

DList.java 这包含方法,我们得到了前 4 个方法,最后 4 个我们必须自己编写,坦率地说,我刚刚弄糊涂了。运行程序时出现此错误

Exception in thread "main" java.lang.NullPointerException
    at javaapplication13.DList.remove(Dlist.java:41)
    at javaapplication13.Driver.main(Driver.java:18)
Java Result: 1

我知道这个错误是什么,但我觉得我最终实施了错误的方法,而且我的教授没有直接通过示例。

DList./java,这些是方法

package javaapplication13;

import java.util.NoSuchElementException;

/**
 *
 * @author thomas
 */
class DList<E> {
    public DLink<E>  head , tail , curr ;
    private  int  cnt ;



  /**
     * Insert "it" at current position
     */
    public void insert(E it) {
        curr.setNext(new DLink<E>(it, curr, curr.next()));
        curr.next().next().setPrev(curr.next());
        cnt++;
    }

    /**
     * Append "it" to list
     */
    public void append(E it) {
        tail.setPrev(new DLink<E>(it, tail.prev(), tail));
        tail.prev().prev().setNext(tail.prev());
        cnt++;
    }

    /**
     * Remove and return current element
     */
    public E remove() {
        if (curr.next() == tail) {
            return null; // Nothing to remove
        }
        E it = curr.next().element(); // Remember value
        curr.next().next().setPrev(curr);
        curr.setNext(curr.next().next()); // Remove from list
        cnt--; // Decrement the count
        return it; // Return value removed

    }
 public void moveToPrev() {
        if (curr != head) // Can’t back up from list head
        {
            curr = curr.prev();
        }
    }

 public void moveToPos(int pos)
 {
     assert (pos>=0) && (pos<=cnt) : "Position out of range";
  curr = head;
  for(int i=0; i<pos; i++) {curr = curr.next();
 }}

 public void moveToValue(E it)
 {
while (it!=curr)
{

}
  }
   public  void insertBefore(E it) {
   curr.setNext(new DLink<E>(it, curr, curr.prev()));
        curr.next().prev().setPrev(curr.next());
        cnt++;

  }

 public void removePrev()
 {
moveToPrev();
 if (curr==null) {
      throw new IndexOutOfBoundsException("empty list.");
    }
    if (curr == head) {
      throw new NoSuchElementException("cannot remove the head");
    }
    remove();

  }

 public void printAll()
 {


        while(curr != null){
            System.out.print(curr +" ");
            curr = curr.next;
        }
        System.out.println();          
             }
}

DLink.java,这个也是给我们的。

package javaapplication13;

/**
 * Doubly linked list node
 */
class DLink<E> {

  public E element; // Value for this node
public DLink<E> next, prev; // Pointer to next node in list
 // Pointer to previous node

    /**
     * Constructors
     */

    /** Constructors */
DLink(E it, DLink<E> p, DLink<E> n)
{ element = it; prev = p; next = n; }
DLink(DLink<E> p, DLink<E> n) { prev = p; next = n; }
/** Get and set methods for the data members */
DLink<E> next() { return next; }
DLink<E> setNext(DLink<E> nextval)
{ return next = nextval; }
DLink<E> prev() { return prev; }
DLink<E> setPrev(DLink<E> prevval)
{ return prev = prevval; }
E element() { return element; }
E setElement(E it) { return element = it; }


}

Driver.java ,这是教师提供的用于测试的文件

public class Driver{

public static void main(String args[])
{
    DList<Integer> dlList = new DList<Integer>();

    System.out.println("removing from empty list");
    dlList.remove();
    dlList.remove();

    dlList.insert(30);
    dlList.insert(20);
    dlList.insert(10);

    dlList.append(40);
        dlList.append(50);
        dlList.append(60);

    dlList.printAll();

    System.out.println("Moving to Position 3..");
    dlList.moveToPos(3);

    dlList.append(200);

    dlList.insert(180);
    dlList.insert(140);
    dlList.insert(120);

    System.out.println("Moving to Previous Position..");
    dlList.moveToPrev();

    dlList.printAll();

    System.out.println("Removing node..");
    dlList.remove();

    dlList.printAll();

    System.out.println("Moving to First Position..");
    dlList.moveToStart();

    System.out.println("Removing node..");
    dlList.remove();

    dlList.printAll();

    System.out.println("Moving to Value 2..");
    dlList.moveToValue(2);

    System.out.println("Moving to Value 120..");
        dlList.moveToValue(120);

    System.out.println("Inserting 25..");
    dlList.insert(25);
        dlList.printAll();

    System.out.println("Removing previous node..");
        dlList.removePrev();
        dlList.printAll();

    System.out.println("Moving to First Position..");
    dlList.moveToStart();

    System.out.println("Inserting 45..");
        dlList.insert(45);

    System.out.println("Insert before 35..");
    dlList.insertPrev(35);
        dlList.printAll();

    System.out.println("Moving to 50..");
        dlList.moveToValue(50);

    System.out.println("Inserting 55..");
        dlList.insert(55);
    dlList.printAll();

    System.out.println("Removing previous nodes twice.");
    dlList.removePrev();
        dlList.removePrev();
        dlList.printAll();

    System.out.println("Removing nodes thrice.");
        dlList.remove();
    dlList.printAll();
    dlList.remove();
        dlList.printAll();
        dlList.remove();

        dlList.printAll();

}
}
4

1 回答 1

0

curr.next() 一片空白。你没有初始化它。所以在其中调用一个方法会给出 NullPointerException

于 2013-09-22T02:37:07.623 回答