getConfig from another class null pointer

Discussion in 'Plugin Development' started by Aliano, Oct 17, 2020.

  1. Offline



    i tryed to get String from the Config file and its worked perfectly. I dont know what i changed but now it doesnt work anymore. I tryed all posibilites on bukkit but nothing helped.

    Main (open)
    public final class Main extends JavaPlugin {
        public void onEnable() {
            //Plugin startup logic
            final FileConfiguration config = this.getConfig();
            getCommand("rulesaccept").setExecutor((CommandExecutor)new RulesAccept());
        public void onDisable() {
            // Plugin shutdown logic
            Bukkit.getLogger().fine("Plugin wird deaktiviert");
        private void listenerRegistration() {
            PluginManager pluginManager = Bukkit.getPluginManager();
            this.getServer().getPluginManager().registerEvents(new JoinListener, this);

    Join Listener Class (open)
    public class JoinListener implements Listener {
        public void onJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
           // String configValue= config.getString("" + p.getDisplayName());
            String configValue = Main.getPlugin(Main.class).getConfig().getString(""+ p.getDisplayName());

    error (open)
    [16:49:28 ERROR]: Could not pass event PlayerJoinEvent to Rulesaccept v1.0-SNAPSHOT
    java.lang.NullPointerException: null
    at de.juliaz.rulesaccept.listeners.JoinListener.onJoin( ~[?:?]
    at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor3.execute(Unknown Source) ~[?:?]
    at org.bukkit.plugin.EventExecutor.lambda$create$1( ~[server.jar:git-Paper-189]
    at co.aikar.timings.TimedEventExecutor.execute( ~[server.jar:git-Paper-189]
    at org.bukkit.plugin.RegisteredListener.callEvent( ~[server.jar:git-Paper-189]
    at org.bukkit.plugin.SimplePluginManager.callEvent( ~[server.jar:git-Paper-189]
    at net.minecraft.server.v1_16_R2.PlayerList.postChunkLoadJoin( ~[server.jar:git-Paper-189]
    at net.minecraft.server.v1_16_R2.PlayerList.lambda$null$1( ~[server.jar:git-Paper-189]
    at net.minecraft.server.v1_16_R2.PlayerConnection.tick( ~[server.jar:git-Paper-189]
    at net.minecraft.server.v1_16_R2.NetworkManager.a( ~[server.jar:git-Paper-189]
    at net.minecraft.server.v1_16_R2.ServerConnection.c( ~[server.jar:git-Paper-189]
    at net.minecraft.server.v1_16_R2.MinecraftServer.b( ~[server.jar:git-Paper-189]
    at net.minecraft.server.v1_16_R2.DedicatedServer.b( ~[server.jar:git-Paper-189]
    at net.minecraft.server.v1_16_R2.MinecraftServer.a( ~[server.jar:git-Paper-189]
    at net.minecraft.server.v1_16_R2.MinecraftServer.w( ~[server.jar:git-Paper-189]
    at net.minecraft.server.v1_16_R2.MinecraftServer.lambda$a$0( ~[server.jar:git-Paper-189]
    at Source) [?:1.8.0_261]

    config file is already created. its important for me to get the player name from the config.

    i hope you can help
  2. Offline


    Please show the error and specify what line isn't working.
  3. Offline


    That's an awkward way to get your plugin instance. Just use dependency injection. You'd change the listener registration as such:
    private void listenerRegistration() {
      PluginManager pluginManager = Bukkit.getPluginManager();
      this.getServer().getPluginManager().registerEvents(new JoinListener(this), this);
    Then change your listener class:
    public class JoinListener implements Listener {
      Main plugin;
      public JoinListener(Main plugin) {
        this.plugin = plugin;
    Then you have an instance of the main class right there. As to your actual event code:
    String configValue = Main.getPlugin(Main.class).getConfig().getString(""+ p.getDisplayName())
    Why are you getting "" + display name? The empty string is superfluous. Just get p.getDisplayName. Also bear in mind, if the config does not have that person's name in it at the root level it won't locate it and return null. Either handle that or pass a default value. As it is, any time a new player joins your plugin will puke.

    configValue is a String, it is superfluous to call toString on a String.
    Aliano likes this.
  4. Offline


    Strahan likes this.

Share This Page