这个文件编译没问题了,可以运行的时候老是出现下面的这些错误。小弟实在不明白,因为本人也是刚学了一个星期。可能是少了什么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.   

    if (isEmpty()) // if empty list
    first = newLink; // first-->newLink
    else
    // old last-->newLink
    last.next = newLink; // newLink<--last
    last = last.next; // last<--newLink经典
    这里 last从来没有被赋值过,last一直为null
    那么 last.next=newLink 和 last=last.next 都回报空指针
      

  2.   

    看下修改後的代码
    public void insertLast(int dd) // insert at end of list
    {
    Link newLink = new Link(dd);
    if (isEmpty())
    first = newLink;
    else
    last.next = newLink;
    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经典
    }
    改为
    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 = last.next; // last<--newLink经典
    }