NullPointerException towards Config

Discussion in 'Plugin Development' started by CaveBoy36, Nov 29, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    CaveBoy36

    Hello! I am trying to make a simple random teleport plugin but it's having issues finding the integer in the config. Here is the class:
    Code:
    public class RandomTeleport {
        static CorePluginsMain plugin;
        
        public RandomTeleport(CorePluginsMain instance) {
        plugin = instance;
        }
    public static void Teleport(Player p) {
     (17)   int max = plugin.getConfig().getInt("settings.randomteleportmax");
        int x = -max + (int)(Math.random()*max);
        int z = -max + (int)(Math.random()*max);
        int y = p.getWorld().getHighestBlockYAt(x, z);
        Location loc = new Location(p.getWorld(), x, y, z);
        p.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 10, 10));
        p.teleport(loc);
        p.sendMessage(ChatColor.GRAY + "Teleporting...");
        return;
    }
    }
    Here is the config:
    Code:
    settings:
      randomteleportmax: 10000
    Here is the error:
    Code:
    29.11 16:37:15 [Server] INFO ... 17 more
    29.11 16:37:15 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[custom18.jar:git-Spigot-db6de12-18fbb24]
    29.11 16:37:15 [Server] INFO at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_45]
    29.11 16:37:15 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_45]
    29.11 16:37:15 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_45]
    29.11 16:37:15 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_45]
    29.11 16:37:15 [Server] INFO at me.CaveBoy36.Listeners.RandomTeleportListeners.PlayerInteractionEvent(RandomTeleportListeners.java:59) ~[?:?]
    29.11 16:37:15 [Server] INFO at me.CaveBoy36.CorePlugins.RandomTeleport.Teleport(RandomTeleport.java:17) ~[?:?]
    29.11 16:37:15 [Server] INFO Caused by: java.lang.NullPointerException
    29.11 16:37:15 [Server] INFO at java.lang.Thread.run(Thread.java:745) [?:1.8.0_45]
    29.11 16:37:15 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [custom18.jar:git-Spigot-db6de12-18fbb24]
    29.11 16:37:15 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [custom18.jar:git-Spigot-db6de12-18fbb24]
    29.11 16:37:15 [Server] INFO at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [custom18.jar:git-Spigot-db6de12-18fbb24]
    29.11 16:37:15 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [custom18.jar:git-Spigot-db6de12-18fbb24]
    29.11 16:37:15 [Server] INFO at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [custom18.jar:git-Spigot-db6de12-18fbb24]
    29.11 16:37:15 [Server] INFO at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_45]
    29.11 16:37:15 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_45]
    29.11 16:37:15 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [custom18.jar:git-Spigot-db6de12-18fbb24]
    29.11 16:37:15 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [custom18.jar:git-Spigot-db6de12-18fbb24]
    29.11 16:37:15 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [custom18.jar:git-Spigot-db6de12-18fbb24]
    29.11 16:37:15 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:759) [custom18.jar:git-Spigot-db6de12-18fbb24]
    29.11 16:37:15 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInteractManager.java:463) [custom18.jar:git-Spigot-db6de12-18fbb24]
    29.11 16:37:15 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:228) [custom18.jar:git-Spigot-db6de12-18fbb24]
    29.11 16:37:15 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [custom18.jar:git-Spigot-db6de12-18fbb24]
    29.11 16:37:15 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [custom18.jar:git-Spigot-db6de12-18fbb24]
    29.11 16:37:15 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[custom18.jar:git-Spigot-db6de12-18fbb24]
    29.11 16:37:15 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[custom18.jar:git-Spigot-db6de12-18fbb24]
    29.11 16:37:15 [Server] INFO org.bukkit.event.EventException
    29.11 16:37:15 [Server] ERROR Could not pass event PlayerInteractEvent to CorePlugins v1.0
    And I've tried replacing the getConfig().getInt thing with 10000, and it works, it's just I'm wanting to figure out why the config part isn't working. Also, I've double checked the config and made sure it was there. I'm not sure what's going on. Any help would be appreciated!
     
  2. Offline

    teej107

    @CaveBoy36 What exact line is it happening? Btw remove your static declarations. You are using them terribly and wrong and I bet you anything that the NPE is happening as a result of that.
     
  3. Offline

    Xerox262

    Are you creating an instance of the class? So that the plugin field is actually assigned? If not then that is your problem, not the config.

    He pointed it out. its the very first line of the method.
    (17)int max = plugin.getConfig().getInt("settings.randomteleportmax");
     
  4. Offline

    tommyhoogstra

    Why are you using static for these methods, even though you have a constructor?

    It won't fix the issue im pretty sure, but it does reflect your java knowledge.
    In the class you have provided, remove the static modifier, and create a class access variable in coremainplugin
     
  5. Offline

    Xerox262

    It would fix the error, he's not calling the constructor. It's the plugin field that is null, not the config.
     
  6. Offline

    tommyhoogstra

    Not using static should encourage him to construct the class
     
  7. Offline

    CaveBoy36

    Yes, I am still kinda new to some of this, that's why I'm trying to make a simple version of it. :p Also, the reason I have static was to call the function in another class. Is that wrong? I have it calling it on an interaction class when the player clicks the sign. Here is the line calling it:
    Code:
                    RandomTeleport.Teleport(p);
    
    Thanks for the fast responses btw!
     
  8. Offline

    teej107

    It's for the wrong reason because that is not what static is for. So yes, that is wrong.
     
  9. Offline

    tommyhoogstra

    Ill put it straight,

    You want to create an instance of the RandomTeleport class in CorePluginMain like so

    CorePluginMain.java
    Code:
    private RandomTeleport rt;
    
    @Override
    public void onEnable(){
          getServer().getPluginManager().registerEvents(this,this);
          rt = new RandomTeleport(this); // WE pass this as an argument as your RandomTeleport class needs access to it.
    }
    
    
    Now to access teleport(Player p), you don't need to write RandomTeleport.Teleport(p)
    You can now just do, rt.Teleport(p);
     
  10. Offline

    teej107

    @tommyhoogstra Don't forget about getters if he needs to access it from a class other than his JavaPlugin extended one. Encapsulation FTW! Of course you should already know about that since it's basic Java :p @CaveBoy36
     
    tommyhoogstra likes this.
  11. Offline

    CaveBoy36

    @tommyhoogstra I'll try that! Do I still access the CorePluginsMain with
    Code:
         CorePluginsMain plugin;
        
        public RandomTeleportListeners(CorePluginsMain instance) {
        plugin = instance;
        }
    Then I could use that with plugin.getConfig() and stuff? Thank you again for helping. Not everybody is perfect @teej107
     
  12. Offline

    Mrs. bwfctower

    Sure. But if you started with the Bukkit API with the required amount of Java knowledge you wouldn't be needing this kind of help (Java help).

    Please direct your attention to http://bukkit.org/threads/plugin-dev-sticky-learning-java-where-to-learn.395662/ where there are a number of places listed to learn Java.

    The default access modifier allows access from the same package, so you may as well just make this field private.
     
  13. Offline

    tommyhoogstra


    Yeah you will be able to access things the same way just about,
     
Thread Status:
Not open for further replies.

Share This Page