Warning: file_put_contents(/datas/wwwroot/jiajiahui/core/caches/caches_template/2/default/show.php): failed to open stream: Permission denied in /datas/wwwroot/jiajiahui/core/libraries/classes/template_cache.class.php on line 55

Warning: chmod(): Operation not permitted in /datas/wwwroot/jiajiahui/core/libraries/classes/template_cache.class.php on line 56
为什么我收到java.lang.NullPointerException错误 - 源码之家

为什么我收到java.lang.NullPointerException错误

问题描述:

每当我调用pennyCount方法或removePenny方法,我得到一个空指针异常错误,我不明白,因为我的HashSet应该填充在构造函数中。为什么我得到这个,我该如何解决它?为什么我收到java.lang.NullPointerException错误

import java.util.HashSet; 

public class Pocket 
{ 

private HashSet<Penny> penniesSet; 



public Pocket(int numOfPennies){ 

    HashSet<Penny> penniesSet = new HashSet<Penny>(); 

    for(int n = 0; n < numOfPennies; n++){ 
     penniesSet.add(new Penny());} 



} 

public int pennyCount(){ 
    return penniesSet.size(); 

} 

public Penny removePenny(){ 

    if(penniesSet.size() == 0){ 
     return null; 
    } 
    else{ 
     Penny toRemove = penniesSet.iterator().next(); 
     penniesSet.remove(toRemove); 
     return toRemove; 

    } 
} 

}

+0

东西是空的。所以你得到NullPointerException。:) – subash 2014-12-02 14:59:26

在构造函数中,你有以下

HashSet<Penny> penniesSet = new HashSet<Penny>(); 

应该

penniesSet = new HashSet<Penny>(); 

在你的构造你宣布一个新的HashSet penniesSet,它应该是:

public Pocket(int numOfPennies){ 

     penniesSet = new HashSet<Penny>(); 

     for(int n = 0; n < numOfPennies; n++){ 
      penniesSet.add(new Penny());} 
} 

否则你是pennieSet永远不会初始化。

尝试

public Pocket(int numOfPennies){ 

    penniesSet = new HashSet<Penny>(); // was HashSet<Penny> penniesSet = new HashSet<Penny>(); 

    for(int n = 0; n < numOfPennies; n++){ 
     penniesSet.add(new Penny());} 



} 
+0

你的答案看起来不错,但你可以添加一个解释如何解决OP问题? – thegrinner 2014-12-02 15:38:45

+0

我的答案是每个人都在一起,有什么问题? – outdev 2014-12-02 15:57:54

+0

他们大多数都会详细讨论为什么改变这条线可以修复它(也就是说,当OP重新声明它时,该变量是被遮蔽的)。我评论你的答案,因为我在评论队列中看到它(因为它主要是代码)。只是一个FYI :) – thegrinner 2014-12-02 16:00:50

改变这样的。

public Pocket(int numOfPennies){ 

    penniesSet = new HashSet<Penny>(); 

    for(int n = 0; n < numOfPennies; n++){ 
     penniesSet.add(new Penny());} 

} 
+1

这将是很好,如果你解释,OP的问题是什么,以及如何解决它在这里。 – blalasaadri 2014-12-02 15:27:21

变化

public Pocket(int numOfPennies){ 

    HashSet<Penny> penniesSet = new HashSet<Penny>(); 

    for(int n = 0; n < numOfPennies; n++){ 
     penniesSet.add(new Penny());} 



} 

public Pocket(int numOfPennies){ 

    penniesSet = new HashSet<Penny>(); //set the instance variable instead of creating a local variable 

    for(int n = 0; n < numOfPennies; n++){ 
     penniesSet.add(new Penny());} 



} 

您在构造函数中创建一套新的对象,而不是填充类的领域。尝试:

penniesSet = new HashSet<Penny>(); 

您在构造函数中创建两个具有相同名称的字段和一个本地HashSet。会发生什么是局部变量将被实例化。到达pennyCount()时,该字段仍然为空。

private HashSet<Penny> penniesSet; //first here 

public Pocket(int numOfPennies){ 
    HashSet<Penny> penniesSet = new HashSet<Penny>(); //then here 

要更正这样做。

private HashSet<Penny> penniesSet; 

public Pocket(int numOfPennies){ 
    penniesSet = new HashSet<Penny>();