[Help]NullPointerException

Discussion in 'Programming' started by Soulcraft245, Aug 11, 2017.

  1. hello, I'm getting an error over NullPointerException.

    This is the whole error together:

    Code (Text):
    java.lang.NullPointerException: null
            at com.Soulcrafter245.badxXFireXWolfXx.playerset.<init>(playerset.java:20) ~[?:?]
            at com.Soulcrafter245.badxXFireXWolfXx.firewolfsetup.registeronworldchange(firewolfsetup.java:25) ~[?:?]
            at com.Soulcrafter245.badxXFireXWolfXx.firewolfsetup.onEnable(firewolfsetup.java:13) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot-1.12.1.jar:git-Spigot-5340a52-27b8bf9]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot-1.12.1.jar:git-Spigot-5340a52-27b8bf9]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:402) [spigot-1.12.1.jar:git-Spigot-5340a52-27b8bf9]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:380) [spigot-1.12.1.jar:git-Spigot-5340a52-27b8bf9]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:329) [spigot-1.12.1.jar:git-Spigot-5340a52-27b8bf9]
            at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:422) [spigot-1.12.1.jar:git-Spigot-5340a52-27b8bf9]
            at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:383) [spigot-1.12.1.jar:git-Spigot-5340a52-27b8bf9]
            at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:338) [spigot-1.12.1.jar:git-Spigot-5340a52-27b8bf9]
            at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:272) [spigot-1.12.1.jar:git-Spigot-5340a52-27b8bf9]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:545) [spigot-1.12.1.jar:git-Spigot-5340a52-27b8bf9]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_111]
    here my code for class firewolfsetup:
    Code (Text):
    public class firewolfsetup  extends JavaPlugin {
       
        public FileConfiguration cfg;
        public Object plugin;

        public void onEnable(){
            registeronworldchange();
            registerCongfig();  
        }
       
        private void registerCongfig() {
            getConfig().options().copyDefaults(true);
            saveConfig();
        }

        public void registeronworldchange() {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(new playerset(), this);
        }

        public void onDisable(){
           
        }  
    }
    here my class for playerset:
    Code (Text):
    public class playerset implements Listener {

        public static firewolfsetup plugin;
        public void playersets(firewolfsetup plugin) {
            this.plugin = plugin;
          }
       
        List<String> list = plugin.cfg.getStringList("Players");
        String[] playertype = list.toArray(new String[0]);
       
        @EventHandler
         private void onworldchange(PlayerChangedWorldEvent event){
             System.out.println(playertype);  
           }
    }
    also if someone can tell me if, I'm setting up the Array right to get a list of Strings in the congif
     
  2. MiniDigger

    Supporter

    you have to make the method playersets into a constructor, and initialize your list in the constructor too.
    you also should make your plugin var in playerset private and remove the static.
     
  3. I'm thinking this is what you mean?
    Code (Text):
    public class playerset implements Listener {
       
        private firewolfsetup plugin;
        public playerset(String[] playertype) {
            List<String> list = plugin.cfg.getStringList("Players");
            playertype = list.toArray(new String[0]);
          }
       
        @EventHandler
         private void onworldchange(PlayerChangedWorldEvent event){
             System.out.println(playerset.this);
           }
       
    }
     
  4. You need to go back and take some more time learning Java before you start using the Spigot API. Here are some things that you need to learn:

    Encapsulation
    Access modifiers
    Static
    Scope
    Java conventions (optional)