我在构造函数中初始化了一个变量,但是当我尝试在另一个方法中使用它时它仍然是null

时间:2016-06-23 13:04:57

标签: java nullpointerexception bukkit

所以这是构造函数

private ActionbarTitleObject timer = TitleFactory.newBarTitle("");
private Plugin plugin;
HashMap<String, Parkour> parkours = null;

public MineJump() {}

public MineJump(ArrayList<Player> players, String configPath, Plugin plugin)
{
    super(players);
    this.plugin = plugin;
    parkours = Parkour.load(configPath, players);
    System.out.println(parkours.get("p1").getCurrentSpawn()); //here I get the right value
}

但是当我尝试在此方法中使用parkours哈希图时

@EventHandler
public void onPlayerMove(PlayerMoveEvent e)
{
    System.out.println(parkours.get("p1").getCurrentSpawn()); //parkours is null here :(
}

我收到NullPointerException。那是为什么?

我只使用非参数构造函数:Bukkit.getPluginManager().registerEvents(new MineJump(), this);

这会导致错误吗?如果是的话,如果你能向我解释原因,那就太好了。 :)

2 个答案:

答案 0 :(得分:1)

正如您所解释的那样,您使用了无参数构造函数。此构造函数不会将parkours设置为与null不同的值。

你需要使用带有这样的参数的第二个构造函数:

Bukkit.getPluginManager().registerEvents(new MineJump(players, configPath, plugin), this);

答案 1 :(得分:0)

可能的解释是,在构造函数中调用super()时,val df=sc.parallelize(Seq( (1,"example1", Seq(0,2,5)), (2,"example2", Seq(1,20,5)))).toDF("column1", "column2", "column3") 被称为副作用结果