继续在链表中获取空指针异常

时间:2014-10-18 01:56:00

标签: java nullpointerexception linked-list

我试图从链表中删除一个特定的字符串,但我一直得到空指针异常错误!任何帮助家伙?

public void delete(String deleteName) {

        LinkedListNode current = head;
        if(head==null){
            System.out.println("Nothing to delete from the list! Insert data.");
        }
        // for head
        else if(current.getName()==deleteName){
            head=current.getNext();
        }
        // for tail
        else if(findTail().getName() == deleteName){
            while(current.getNext().getNext()!=null ){
                current = current.getNext();
            }
            current.setNext(null);
        }
        else{
            while(current.getNext().getName()!=deleteName){
                current=current.getNext();
            }
            current.setNext(current.getNext().getNext());
        }
        System.out.println("");
        System.out.println(deleteName +" has been deleted from the list.");

    }

1 个答案:

答案 0 :(得分:0)

做了一些编辑。现在工作!不要使用==来比较字符串。

public void delete(String deleteName) {



        LinkedListNode current = head;
        if(head==null){
            System.out.println("Nothing to delete from the list! Insert data.");
        }
        // for head
        else if(current.getName().equals(deleteName)){
            head=current.getNext();
        }
        // for tail
        else if(findTail().getName().equals(deleteName)){
            while(current.getNext().getNext()!=null ){
                current = current.getNext();
            }
            current.setNext(null);
        }
        else{
            while(!current.getNext().getName().equals(deleteName)){
                current=current.getNext();
            }
            current.setNext(current.getNext().getNext());
        }
        System.out.println("");
        System.out.println(deleteName +" has been deleted from the list.");

    }   // end of delete()