这个文件编译没问题了,可以运行的时候老是出现下面的这些错误。小弟实在不明白,因为本人也是刚学了一个星期。可能是少了什么throws语句,实在郁闷了。弄了一个小时没弄出来。
错误如下:
Exception in thread "main" java.lang.NullPointerException
at FirstLastList.insertLast(linkQueue.java:50)
at LinkQueue.insert(linkQueue.java:95)
at LinkQueueApp.main(linkQueue.java:114)
源代码如下:
//linkQueue.java
//demonstrates queue implemented as double-ended list
//to run this program: C:>java LinkQueueApp
class Link {
public int dData; // data item
public Link next; // next link in list // -------------------------------------------------
public Link(int d) // constructor
{
dData = d;
} // -------------------------------------------------
public void displayLink() // display this link
{
System.out.print(dData + " ");
}
// ------------------------------------------------
} // end class Link///////////////////////////////////////////////////////////
class FirstLastList {
private Link first; // ref to first item private Link last; // ref to last item // ------------------------------------------------
public FirstLastList() // constructor
{
first = null;
last = null; // no items on list yet
} // -------------------------------------------------
public boolean isEmpty() // true if no links
{
return (first == null);
} // ---------------------------------------------------
public void insertLast(int dd) // insert at end of list
{
Link newLink = new Link(dd); // make new link
if (isEmpty()) // if empty list
first = newLink; // first-->newLink
else
// old last-->newLink
last.next = newLink; // newLink<--last
last = last.next; // last<--newLink经典
} // ---------------------------------------------------
public int deleteFirst() // delete first link
{ // assumes non-empty list
int temp = first.dData;
if (first.next == null) // if only one item
last = null; // null<--last
first = first.next; // first-->old next
return temp; // 有点问题想不明白
} // ----------------------------------------------------------
public void displayList() {
Link current = first; // start at beginning
while (current != null) // until end of list
{
current.displayLink(); // print data
current = current.next; // move to next link
}
System.out.println();
}
// -------------------------------------------------
}// end class FirstLastList/////////////////////////////////////////////////////////////////////
class LinkQueue {
private FirstLastList theList; // -----------------------------------------------------
public LinkQueue() // constructor
{
theList = new FirstLastList(); // make a 2-ended list
} // ---------------------------------------------------
public boolean isEmpty() // true if queue is empty
{
return theList.isEmpty();
} // --------------------------------------------------------
public void insert(int j) // insert, rear of queue
{
theList.insertLast(j);
} // ------------------------------------------------------------
public int remove() {
return theList.deleteFirst();
} // ----------------------------------------------------
public void displayQueue() {
System.out.println("Queue (first-->rear):");
theList.displayList();
}
}// end class LinkQueue///////////////////////////////////////////////////////////////////////
class LinkQueueApp {
public static void main(String[] args) {
LinkQueue theQueue = new LinkQueue();
theQueue.insert(20); // insert item
theQueue.insert(40); theQueue.displayQueue(); // display queue theQueue.insert(60); // insert item
theQueue.insert(80); theQueue.displayQueue(); // display queue theQueue.remove(); // remove item
theQueue.remove(); theQueue.displayQueue(); // display queue
}// end main
}// end class LinkQueueApp

解决方案 »

  1.   

    FirstLastList 类里的
    private Link last//这个last一直为nullpublic void insertLast(int dd){
    Link newLink = new Link(dd); // 
    if (isEmpty()) // isEmpty()返回true
    first = newLink; // 
    else
    last.next = newLink; // 
    last = last.next; // last 为 null抛出NullPointerException
    }
      

  2.   

    public void insertLast(int dd) // insert at end of list
    {
    Link newLink = new Link(dd); // make new link
    if (isEmpty()) // if empty list
    {
    first = newLink; // first-->newLink
    last = first;
    }
    else
    { // old last-->newLink
    last.next = newLink; // newLink<--last
     // last<--newLink经典
    }
    }
      

  3.   

    解答:
    public void insertLast(int dd) // insert at end of list
    {
    Link newLink = new Link(dd); // make new link if (isEmpty()) // if empty list
    first = newLink; // first-->newLink
    else
    // old last-->newLink
    last.next = newLink; // newLink<--last
    last = last.next; // last<--newLink经典
    }
    增加第一个结点first引用这个结点,但last为null,所以运行到last=last.next-->last.next报错.因为last为null;
      

  4.   

    AWUSOFT,我给分的时候你才贴出来,所以给了你5分。抱歉!
      

  5.   

    真的很抱歉,下次吧。我发了帖子通知你^_^
    刚学java一个星期,问题多着呢-_#