一个不明不白的问题,希望各位大侠伸出你们的援助之手!
这个文件编译没问题了,可以运行的时候老是出现下面的这些错误。小弟实在不明白,因为本人也是刚学了一个星期。可能是少了什么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
[解决办法]
FirstLastList 类里的
private Link last//这个last一直为null
public 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
}
[解决办法]
解答:
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;