getConfig from another class null pointer

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

  1. Offline

    Aliano

    Hello,

    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)
    Code:
    public final class Main extends JavaPlugin {
    
    
    
        @Override
        public void onEnable() {
            //Plugin startup logic
    
            final FileConfiguration config = this.getConfig();
            config.options().copyDefaults(true);
            saveConfig();
    
            listenerRegistration();
    
            getCommand("rulesaccept").setExecutor((CommandExecutor)new RulesAccept());
    
        }
    
        @Override
        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)
    Code:
    public class JoinListener implements Listener {
    
    
        @EventHandler
        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());
    
            p.sendMessage(configValue.toString());
    
    
        }
    }


    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(JoinListener.java:33) ~[?:?]
    at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor3.execute(Unknown Source) ~[?:?]
    at org.bukkit.plugin.EventExecutor.lambda$create$1(EventExecutor.java:69) ~[server.jar:git-Paper-189]
    at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:80) ~[server.jar:git-Paper-189]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[server.jar:git-Paper-189]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:607) ~[server.jar:git-Paper-189]
    at net.minecraft.server.v1_16_R2.PlayerList.postChunkLoadJoin(PlayerList.java:273) ~[server.jar:git-Paper-189]
    at net.minecraft.server.v1_16_R2.PlayerList.lambda$null$1(PlayerList.java:220) ~[server.jar:git-Paper-189]
    at net.minecraft.server.v1_16_R2.PlayerConnection.tick(PlayerConnection.java:154) ~[server.jar:git-Paper-189]
    at net.minecraft.server.v1_16_R2.NetworkManager.a(NetworkManager.java:385) ~[server.jar:git-Paper-189]
    at net.minecraft.server.v1_16_R2.ServerConnection.c(ServerConnection.java:145) ~[server.jar:git-Paper-189]
    at net.minecraft.server.v1_16_R2.MinecraftServer.b(MinecraftServer.java:1383) ~[server.jar:git-Paper-189]
    at net.minecraft.server.v1_16_R2.DedicatedServer.b(DedicatedServer.java:375) ~[server.jar:git-Paper-189]
    at net.minecraft.server.v1_16_R2.MinecraftServer.a(MinecraftServer.java:1211) ~[server.jar:git-Paper-189]
    at net.minecraft.server.v1_16_R2.MinecraftServer.w(MinecraftServer.java:999) ~[server.jar:git-Paper-189]
    at net.minecraft.server.v1_16_R2.MinecraftServer.lambda$a$0(MinecraftServer.java:177) ~[server.jar:git-Paper-189]
    at java.lang.Thread.run(Unknown 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

    Kars

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

    Strahan

    That's an awkward way to get your plugin instance. Just use dependency injection. You'd change the listener registration as such:
    Code:
    private void listenerRegistration() {
      PluginManager pluginManager = Bukkit.getPluginManager();
      this.getServer().getPluginManager().registerEvents(new JoinListener(this), this);
    }
    Then change your listener class:
    Code:
    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:
    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.

    Code:
    p.sendMessage(configValue.toString());
    configValue is a String, it is superfluous to call toString on a String.
     
    Aliano likes this.
  4. Offline

    Aliano

    Strahan likes this.

Share This Page