Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 这个输出正确吗?还是一个错误?_Java_Eclipse_Debugging_Exception_Nullpointerexception - Fatal编程技术网

Java 这个输出正确吗?还是一个错误?

Java 这个输出正确吗?还是一个错误?,java,eclipse,debugging,exception,nullpointerexception,Java,Eclipse,Debugging,Exception,Nullpointerexception,我只是想确定我从Eclipse接收到的是一个错误,还是没有问题? 这是我的密码: package prj2; public class Sandbox { public static void main(String[] args) { NodeChain<Integer> myList = new NodeChain<Integer>(); myList.add(1); myList.add(2);

我只是想确定我从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());
    }

}
如果这是一个错误,你能帮助修复吗

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();
    }
}
包prj2;
公共类NodeChain实现ListADT{
私有ListNode头;//这是一个伪头
私有节点尾部;
私有内部网络;
公共NodeChain(){
head=新的ListNode(空);
尾=头;
numItems=0;
}
公共布尔值isEmpty(){return numItems==0;}
新增(E项){
//请注意,不需要空检查
tail.setNext(新的ListNode(item,tail.getNext());
tail=tail.getNext();
numItems++;
}
公共作废添加(内部pos,E项){
//处理提供无效pos的情况
如果(pos<0 | | pos>numItems)抛出新的IndexOutOfBoundsException();
//处理我们要添加到列表末尾的情况。
如果(pos==numItems){
增加(项目);
返回;
}
//无需其他特殊情况处理
//遍历列表,直到到达插入点并执行插入。
ListNode tmp=traverseTo(pos);
setNext(新的ListNode(item,tmp.getNext());
//增量单位
numItems++;
}
public int size(){return numItems;}
公共E删除(int pos){//未实现
返回null;
}
公共E-get(内部位置){
如果(pos<0 | | pos>=numItems)抛出新的IndexOutOfBoundsException();
ListNode节点=traverseTo(位置+1);
返回node.getData();
}
公共布尔包含(E obj){
ListNode tmp=头部;
while(tmp!=null){
if(tmp.getData().equals(obj))返回true;
tmp=tmp.getNext();
}
返回false;
}
私有ListNode traverseTo(int pos){
如果(pos<0 | | pos>=numItems)抛出新的IndexOutOfBoundsException();
ListNode tmp=头部;
对于(int i=0;i
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; }
}
包prj2;
公共类列表节点{
私有T数据;
私有listnodenext;
公共列表节点(T obj){
这(obj,null);
}
公共ListNode(T对象,ListNode ptr){
数据=obj;
next=ptr;
}
public void setData(T obj){data=obj;}
public T getData(){return data;}
public void setNext(listnoden){next=n;}
public ListNode 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);
}
包prj2;
/**
*列表是项目的有序集合。
*/
公共接口列表{
/**
*将项目添加到列表的末尾。
*
*@param item要添加的项
*/
无效添加(E项);
/**
*在列表中的pos位置添加项目,移动项目
*最初在位置pos到尺寸()-1一处
*在右边腾出空间。
*
*@param pos添加项目的位置
*@param item要添加的项
*@如果pos小于0,则引发IndexOutOfBoundsException
*或大于大小()
*/
无效添加(int pos,E项);
/**
*返回真iff项为
*清单中的第x项:
*
*@param item将项目添加到
*@return true,如果项目为
*/
布尔包含(E项);
/**
*返回列表中的项目数。
*
*@返回列表中的项目数
*/
int size();
/**
*如果列表为空,则返回true。
*
*@如果列表为空,则返回true,否则返回false
*/
布尔isEmpty();
/**
*返回列表中位置pos处的项目。
*
*@param pos要返回的项目的位置
*@在位置pos返回项目
*@如果pos小于0,则引发IndexOutOfBoundsException
*或大于或等于大小()
*/
E get(int-pos);
/**
*移除并返回列表中位置处的项目,
*将原先位于位置pos+1至的项目移动
*size()左侧一个位置以填充间隙。
*
*@param pos移除项目的位置
*@在位置pos返回项目
*@如果pos小于0,则引发IndexOutOfBoundsException
*或大于或等于大小()
*/
E移除(内部位置);
}

从输出中可以清楚地看出,这是一个抛出的异常,或者更确切地说是一个
错误。抛出的异常是
java.lang.NullPointerException

此外,错误的堆栈跟踪打印在异常抛出行的下面:-

at prj2.NodeChain.contains(NodeChain.java:57)
at prj2.Sandbox.main(Sandbox.java:18)
另外,从堆栈跟踪中可以清楚地看到,在
System.out.println(myList.contains(45))中抛出了一个异常


在检查
NodeClass
类中
contains()
方法的代码后,最好解决此错误

这是一个错误,没有
NodeChain
的源代码,不可能知道为什么stacktrace似乎能提供您所需的所有信息。我无法想象很多时候,
NullPointerException
+stack trace+程序出口是程序的正确输出…@MadProgrammer我在更新的问题中添加了其余的代码。你能看一下吗?很抱歉,这会很混乱,但你使用的是自动格式化的IDE,它应该可以修复它
public void add(E item){//注意不需要空检查ListNode next=null;if(head==null){head=new ListNode(item);tail=head;next
at prj2.NodeChain.contains(NodeChain.java:57)
at prj2.Sandbox.main(Sandbox.java:18)