2017-02-17 14 views
0

我想要使一個LinkedSet對象類實現一個修改後的Set接口。當我嘗試檢查firstNode是否指向null時,我得到一個NullPointerException。我不確定如何解決這個問題。獲取NullPointer實現LinkedSet

這是相關的代碼。

構造整體設置對象

public class LinkedSet<T> implements Set<T> { 

    private Node firstNode; 

    public LinkedSet() { 
     firstNode = null; 
    } // end Constructor 

方法是抱着我

public int getSize() { 
    int size = 1; 
    Node current = firstNode; 

    while ((current.next) != null) { 
     size++; 
     current = current.next; 
    } 
    return size; 
} // end getSize() 

的isEmpty()方法

public boolean isEmpty() { 
    Node next = firstNode.next; //Get error here 
    if (next.equals(null)) { 
     return true; 
    } 
    return false; 
} // end isEmpty() 

這裏是節點的專用內部類對象

private class Node { 
    private T data; 
    private Node next; //Get Error here 

    private Node(T data, Node next) { 
     this.data = data; 
     this.next = next; 
    } // end Node constructor 

    private Node(T data) { 
     this(data, null); 
    }// end Node constructor 
} // end Node inner Class 

最後這裏是主要的測試方法。

public class SetTester { 

    public static void main(String[] args) { 
     LinkedSet<String> set = new LinkedSet<String>(); 
     System.out.println(set.getSize()); //Get error here 
    } 
} 
+2

firstNode = NULL ;在你的構造函數拋出 –

+1

除了其他建議:在'getSize()'我相信你應該初始化'size'爲0並使用'current!= null'作爲'while'條件。 –

回答

0

public class LinkedSet<T> implements Set<T> { 

    private Node firstNode; 

    public LinkedSet() { 
     firstNode = null; 
    } // end Constructor 

firstNode爲空,你是不是初始化存儲器的節點和訪問它afterwards.That是你得到空指針異常,因爲你正在訪問空的原因。將其更改爲。

public class LinkedSet<T> implements Set<T> { 
private Node firstNode; 

public LinkedSet() { 
    firstNode = new Node(); 
} // end Constructor 

要檢查是否爲空

public boolean isEmpty() { 
    return firstNode==null; 
} // end isEmpty() 

節點類

private class Node { 
    private T data; 
    private Node next; //Get Error here 
    private Node(T data, Node next) { 
     next= new Node(); 
     this.data = data; 
     this.next = next; 
    } // end Node constructor 

    private Node(T data) { 
     this(data, null); 
    }// end Node constructor 
} // end Node inner Class 

主要

public class SetTester { 

    public static void main(String[] args) { 
     LinkedSet<String> set = new LinkedSet<String>(); 
     System.out.println(set.isEmpty()); 
    } 
} 
+1

這不會使this.isEmpty()總是false,但? –

1

您需要檢查是否firstNodenull嘗試之前因爲你使用null來初始化它,所以在錯誤的行中訪問它。

4

如果沒有節點,則您的設置爲空。因此,您的isEmpty()實現是您的問題,因爲它假定您始終擁有firstNode,即使您在構造函數中明確將其設置爲null

試試這個:

public boolean isEmpty() { 
    return firstNode == null; 
} 

編輯後的第一個問題被編輯掉:

您仍然可以訪問空(這將導致NullPointerException),因爲你設置currentfirstNode這反過來又從來沒有被設置爲除null外的任何內容。

+0

這實際上修復了這種方法,但我仍然遇到與其他人的麻煩。 –

+2

請不要編輯出有問題的代碼 - 那麼所有的答案看起來都是無稽之談。 – DuneCat

2
public boolean isEmpty() { 
    Node next = firstNode.next; //Get error here 
    if (next.equals(null)) { 
     return true; 
    } 
    return false; 
} // end isEmpty() 

這條線給你NullPointerException異常,我希望:

Node next = firstNode.next; //Get error here 

因爲firstNode可能是null,而不是指向任何地方至今。處理NullPointerException也是最佳做法。所以,你應該做的是:

public boolean isEmpty() { 
    if (firstNode == null) { return true;} 
    return false; 
} // end isEmpty() 

而且,不檢查空爲:

next.equals(null)

經常檢查它:

null == nextnext == null