我只是想确保我从Eclipse收到的是一个错误,还是没关系? 这是我的代码:
package prj2;
public class Sandbox {
public static void main(String[] args) {
NodeChain<Integer> myList = new NodeChain<Integer>();
myList.add(1);
myList.add(2);
myList.add(0, -99);
myList.add(3, 45);
myList.add(99);
myList.add(4, 50);
myList.remove(0);
myList.remove(3);
myList.remove(1);
System.out.println(myList.contains(45));
myList.print();
System.out.println("myList has size "+myList.size());
}
}
结果如下:
Exception in thread "main" java.lang.NullPointerException
at prj2.NodeChain.contains(NodeChain.java:57)
at prj2.Sandbox.main(Sandbox.java:18)
如果这是一个错误,请帮助解决问题吗?
NodeChain的来源:
package prj2;
public class NodeChain<E> implements ListADT<E>{
private ListNode<E> head; // this is a dummy header
private ListNode<E> tail;
private int numItems;
public NodeChain() {
head = new ListNode<E>(null);
tail = head;
numItems = 0;
}
public boolean isEmpty() { return numItems == 0; }
public void add(E item) {
// Note the lack of a need for null checks
tail.setNext(new ListNode<E>(item, tail.getNext()));
tail = tail.getNext();
numItems++;
}
public void add(int pos, E item) {
// Handle the cases where an invalid pos is given
if (pos < 0 || pos > numItems) throw new IndexOutOfBoundsException();
// Handle the case where we're adding to the end of the list.
if (pos == numItems) {
add(item);
return;
}
// No other special case handling required
// Traverse the list until we get to the point of insertion and execute the insertion.
ListNode<E> tmp = traverseTo(pos);
tmp.setNext(new ListNode<E>(item, tmp.getNext()));
// Increment numItems
numItems++;
}
public int size() { return numItems; }
public E remove(int pos) { // Left unimplemented
return null;
}
public E get(int pos) {
if (pos < 0 || pos >= numItems) throw new IndexOutOfBoundsException();
ListNode<E> node = traverseTo(pos+1);
return node.getData();
}
public boolean contains(E obj) {
ListNode<E> tmp = head;
while (tmp != null) {
if (tmp.getData().equals(obj)) return true;
tmp = tmp.getNext();
}
return false;
}
private ListNode<E> traverseTo(int pos) {
if (pos < 0 || pos >= numItems) throw new IndexOutOfBoundsException();
ListNode<E> tmp = head;
for (int i = 0; i < pos; i++)
tmp = tmp.getNext();
return tmp;
}
/* Extra method to facilitate debugging */
public void print() {
ListNode<E> tmp = head;
while (tmp != null) {
System.out.print(tmp.getData()+", ");
tmp = tmp.getNext();
}
System.out.println();
}
}
ListNode的来源:
package prj2;
public class ListNode<T> {
private T data;
private ListNode<T> next;
public ListNode(T obj) {
this(obj, null);
}
public ListNode(T obj, ListNode<T> ptr) {
data = obj;
next = ptr;
}
public void setData(T obj) { data = obj; }
public T getData() { return data; }
public void setNext(ListNode<T> n) { next = n; }
public ListNode<T> getNext() { return next; }
}
ListADT的来源:
package prj2;
/**
* A List is an ordered collection of items.
*/
public interface ListADT<E> {
/**
* Add item to the end of the List.
*
* @param item the item to add
*/
void add(E item);
/**
* Add item at position pos in the List, moving the items
* originally in positions pos through size()- 1 one place
* to the right to make room.
*
* @param pos the position at which to add the item
* @param item the item to add
* @throws IndexOutOfBoundsException if pos is less than 0
* or greater than size()
*/
void add(int pos, E item);
/**
* Return true iff item is
* item x in the List such
*
* @param item the item to
* @return true if item is
*/
boolean contains(E item);
/**
* Return the number of items in the List.
*
* @return the number of items in the List
*/
int size();
/**
* Return true iff the List is empty.
*
* @return true if the List is empty, false otherwise
*/
boolean isEmpty();
/**
* Return the item at position pos in the List.
*
* @param pos the position of the item to return
* @return the item at position pos
* @throws IndexOutOfBoundsException if pos is less than 0
* or greater than or equal to size()
*/
E get(int pos);
/**
* Remove and return the item at position pos in the List,
* moving the items originally in positions pos+1 through
* size() one place to the left to fill in the gap.
*
* @param pos the position at which to remove the item
* @return the item at position pos
* @throws IndexOutOfBoundsException if pos is less than 0
* or greater than or equal to size()
*/
E remove(int pos);
}
答案 0 :(得分:1)
嗯,从输出中可以清楚地看出它是抛出异常,或者更确切地说Error
。抛出的异常是java.lang.NullPointerException
!
此外,错误所在的堆栈跟踪打印在Exception thrown行下面: -
at prj2.NodeChain.contains(NodeChain.java:57)
at prj2.Sandbox.main(Sandbox.java:18)
此外,从堆栈跟踪中可以清楚地看到System.out.println(myList.contains(45));
处抛出异常。
在contains()
类中检查NodeClass
方法的代码后,您最好解决此错误!