一个不明不白的问题,希望各位大侠伸出你们的援助之手!
这个文件编译没问题了,可以运行的时候老是出现下面的这些错误。小弟实在不明白,因为本人也是刚学了一个星期。可能是少了什么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