Search code examples
javaminecraftbukkit

NPE on plugin.getConfig().set(... , ...);


I've a bug on my Spigot Plugin, a null pointer is return when i try to do /samrandom true/false. This is my code : Main class:

package com.vandendaelen.simpleautomessage;

import java.io.File;
import java.util.List;
import java.util.Random;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;

import com.vandendaelen.simpleautomessage.Commands.CommandSamRandom;
import com.vandendaelen.simpleautomessage.Commands.CommandSamTime;

public class SimpleAutoMessage extends JavaPlugin {
    public static final String RANDOM_CONFIG ="Random enabled";
    private int iMessages = 0;

    @Override
    public void onDisable() {
        // TODO Auto-generated method stub
        super.onDisable();
    }

    @Override
    public void onEnable() {
        System.out.println("Waw, an amazing plugin powered by LotuxPunk ! :-)");
        this.getCommand("samtime").setExecutor(new CommandSamTime(this));
        this.getCommand("samrandom").setExecutor(new CommandSamRandom(this, RANDOM_CONFIG));
        createConfig();

        this.getConfig().addDefault(RANDOM_CONFIG, false);
        this.getConfig().options().copyDefaults(true);
        saveConfig();

        //Enable display of messages
        if(getConfig().getBoolean("Enable")) {
            if(getConfig().getBoolean(RANDOM_CONFIG)) {
                messageRandomDisplayer();
            } else {
                messageDisplayer();
            }
        }


    }

    private void createConfig() {
        try {
            if (!getDataFolder().exists()) {
                getDataFolder().mkdirs();
            }
            File file = new File(getDataFolder(), "config.yml");
            if (!file.exists()) {
                getLogger().info("Config.yml not found, creating!");
                saveDefaultConfig();
            } else {
                getLogger().info("Config.yml found, loading!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    ...

}

Command class :

package com.vandendaelen.simpleautomessage.Commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

public class CommandSamRandom implements CommandExecutor {

    private String RANDOM_CONFIG;
    private Plugin plugin;

    public CommandSamRandom(Plugin pl, String r) {
        pl = plugin;
        RANDOM_CONFIG = r;
        //System.out.println(plugin.getConfig().getBoolean(RANDOM_CONFIG));
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        Player p = (Player)sender;
        if(args[0]!="") {
            Boolean randomEnabled = Boolean.parseBoolean(args[0]);
            if(p.hasPermission("simpleautomessage.setrandom")||p.isOp()) {
                plugin.getConfig().set(RANDOM_CONFIG, randomEnabled);

                if(randomEnabled) {
                    p.sendMessage("§2Random enabled");
                } else {
                    p.sendMessage("§4Random disabled");
                }
                plugin.saveConfig();
                plugin.reloadConfig();
                return true;
            }
        }

        return false;
    }

}

Config file :

#Time between 2 messages (minutes)
Time: 15
#Auto-Messages !
Enable: true

#Broadcast randomly your messages
Random enabled: false

Messages:
  - "A simple string"
  - "Another string"
  - "Don't forget to support/rate LotuxPunk on Curse/Bukkit website !"

Result :

[20:56:05 INFO]: LotuxPunk issued server command: /samrandom true
[20:56:05 ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'samrandom' in plugin SimpleAutoMessage v0.5
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-d21162c-61e0c69]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-d21162c-61e0c69]
        ...
Caused by: java.lang.NullPointerException
        at com.vandendaelen.simpleautomessage.Commands.CommandSamRandom.onCommand(CommandSamRandom.java:26) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-d21162c-61e0c69]
        ... 15 more

Anyone can help me please ? :)

The line 26 is

plugin.getConfig().set(RANDOM_CONFIG, randomEnabled);

Solution

  • Your problem is in the Constructor:

    private Plugin plugin;
    
    public CommandSamRandom(Plugin pl, String r) {
        pl = plugin;
    }
    

    You assign the value of your not yet initialized field pluginto the parameter pl insterad of the other way round. It helps to explicitly use this.plugin when you try to access an instance field to make this more obvious.