2017-02-17 66 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