Issues with getting an int from my main class and config

Discussion in 'Plugin Development' started by vYN, Jan 21, 2012.

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

    vYN

    I'm trying to get an int from my main class and use it in my other class. But when i do that. With this code:

    Code:java
    1. int exrad = plugin.getConfig().getInt("Egg explosion radius:", 2);
    2. event.getEgg().getWorld().createExplosion(egg.getLocation(), exrad);


    Then i get this error in the console when the code is executed.

    Code:
    2012-01-22 01:50:48 [INFO] Starting minecraft server version 1.1
    2012-01-22 01:50:48 [INFO] Loading properties
    2012-01-22 01:50:48 [INFO] Starting Minecraft server on *:25565
    2012-01-22 01:50:48 [INFO] This server is running Craftbukkit version git-Bukkit-1.0.1-R1-102-gbc4d4c1-b1796jnks (MC: 1.1) (Implementing API version 1.1-R1-SNAPSHOT)
    2012-01-22 01:50:48 [INFO] [Sewers] Loading Sewers v0.3.2.
    2012-01-22 01:50:48 [INFO] [Sewers] Version 0.3.2 Enabled!
    2012-01-22 01:50:48 [INFO] [Sewers] Usage statistics are being kept for this plugin. To opt-out for any reason, check plugins/PluginStats/config.yml
    2012-01-22 01:50:48 [INFO] Preparing level "world"
    2012-01-22 01:50:48 [INFO] Default game type: 0
    2012-01-22 01:50:48 [INFO] Preparing start region for level 0 (Seed: 543593812937251832)
    2012-01-22 01:50:49 [INFO] Preparing start region for level 1 (Seed: 2097090242839274852)
    2012-01-22 01:50:49 [INFO] Preparing start region for level 2 (Seed: 2097090242839274852)
    2012-01-22 01:50:49 [INFO] Preparing spawn area: 89%
    2012-01-22 01:50:49 [INFO] [ExplodingEggs] Loading ExplodingEggs v1.2.
    2012-01-22 01:50:49 [INFO] ExplodingEggs version 1.2 is now enabled
    2012-01-22 01:50:49 [INFO] ExplodingEggs Author(s): +[GudfareN]
    2012-01-22 01:50:49 [INFO] Server permissions file permissions.yml is empty, ignoring it
    2012-01-22 01:50:49 [INFO] Done (1.147s)! For help, type "help" or "?"
    2012-01-22 01:50:51 [INFO] /127.0.0.1:36749 lost connection
    2012-01-22 01:50:53 [INFO] GudfareN [/127.0.0.1:36754] logged in with entity id 300 at ([world] -4051.90625, 90.09375, -1090.8125)
    2012-01-22 01:50:58 [SEVERE] Could not pass event org.bukkit.event.player.PlayerEggThrowEvent to ExplodingEggs
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$103.execute(JavaPluginLoader.java:1028)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:57)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:327)
        at net.minecraft.server.EntityEgg.a(EntityEgg.java:66)
        at net.minecraft.server.EntityProjectile.y_(EntityProjectile.java:156)
        at net.minecraft.server.World.entityJoinedWorld(World.java:1256)
        at net.minecraft.server.WorldServer.entityJoinedWorld(WorldServer.java:110)
        at net.minecraft.server.World.playerJoinedWorld(World.java:1238)
        at net.minecraft.server.World.tickEntities(World.java:1145)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:527)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:434)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
    Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$103.execute(JavaPluginLoader.java:1026)
        ... 11 more
    Caused by: java.lang.NullPointerException
        at me.GudfareN.ExplodingEggs.Explode.ex(Explode.java:20)
        ... 16 more
    2012-01-22 01:51:02 [INFO] CONSOLE: Stopping the server..
    2012-01-22 01:51:02 [INFO] Stopping server
    2012-01-22 01:51:02 [INFO] [ExplodingEggs] Unloading ExplodingEggs v1.2.
    2012-01-22 01:51:02 [INFO] ExplodingEggs is now disabled
    2012-01-22 01:51:02 [INFO] [Sewers] Unloading Sewers v0.3.2.
    2012-01-22 01:51:02 [INFO] [Sewers] Version 0.3.2 Disabled!
    2012-01-22 01:51:02 [INFO] Saving chunks
    2012-01-22 01:51:03 [INFO] Stopping server
    
     
  2. Offline

    Ranzdo

    It would be easier for us to help if you could post your JavaPlugin class and the Explode class itself. Right now we dont know what it is on line 20 in the Explode class (it is where the null pointer exeption is thrown) neither how you send the instance of the JavaPlugin object to the Explode object (assuming the Explode class is not the JavaPlugin class).
     
  3. Offline

    vYN

    Main.class

    Code:java
    1. public class Main extends JavaPlugin {
    2. public static Main plugin;
    3. public final Logger log= Logger.getLogger("Minecraft");
    4. private Toggle myExecutor;
    5. private Random myExecutor1;
    6.  
    7.  
    8. public void onEnable() {
    9. PluginDescriptionFile pdfFile = this.getDescription();
    10. this.log.info(pdfFile.getName() + " Author(s): +"+ pdfFile.getAuthors());
    11. getServer().getPluginManager().registerEvents(new Explode(), this);
    12. getServer().getPluginManager().registerEvents(new Egg(), this);
    13. getServer().getPluginManager().registerEvents(new BlockListener(), this);
    14.  
    15. this.getConfig().addDefault("Egg_explosion_radius:", 2);
    16. this.getConfig().options().copyDefaults(true);
    17. this.saveConfig();
    18.  
    19.  
    20.  
    21. myExecutor = new Toggle();
    22. getCommand("ee").setExecutor(myExecutor);
    23.  
    24. myExecutor1 = new Random();
    25. getCommand("er").setExecutor(myExecutor1);
    26. }
    27.  
    28. public void onDisable() {
    29. }
    30.  
    31. }


    Explode.class:

    Code:java
    1. public class Explode implements Listener{
    2. public static Main plugin;
    3.  
    4. @EventHandler(priority = EventPriority.NORMAL)
    5. public void ex(PlayerEggThrowEvent event) {
    6. if (Toggle.ee.containsKey(event.getPlayer())){
    7. if (Toggle.ee.containsValue(true)){
    8. int exrad = plugin.getConfig().getInt("Egg_explosion_radius:", 2);
    9. event.getEgg().remove();
    10. Egg egg = event.getEgg();
    11. event.setHatching(true);
    12. event.getEgg().getWorld().createExplosion(egg.getLocation(), exrad);
    13. }else{
    14.  
    15. }
    16. }
    17. }
    18. }
     
  4. Offline

    DirtyStarfish

    Shouldnt this line:
    Code:
    int exrad = plugin.getConfig().getInt("Egg explosion radius:", 2);
    Be this:
    Code:
    int exrad = plugin.getConfig().getInt("Egg explosion radius");
    I dont think your supposed to put the number. It will get the number thats in the file using the path to it.
     
  5. Offline

    vYN

    with the "2" at the end just assigns the default int for it. if there is no int in the config file.
     
  6. Offline

    DirtyStarfish

    You need to have the int in your config file for you to be able to get the value. Thats probably whats causing the NullPointerException. Does your plugin create a config file?
    Also, whats Toggle.ee? Is that a HashMap?
     
  7. Offline

    vYN

    yes it does. I guess. But i think i'm not doing it a good way....
     
  8. Offline

    DirtyStarfish

    It might be worth having a look in the Resources section, there are loads of good tutorials on creating a config file and getting the information. You need to be creating a config file when your first run the plugin, that contains your default values. Once its doing this, you will know that there are values you can grab from it to use in your code.
     
  9. Offline

    vYN

    OK. I'm gone try to make my plugin create a proper config file.

    hmm. my plugin did make a config as it should. And adds the default int that i'm trying to grab.

    this is inside my config.yml when it gets automatically created: "'Egg_explosion_radius:': 2"

    But for some reason it ads ' ' those two... can it be those that are causing the issue?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  10. Offline

    DirtyStarfish

    When you add your defaults under onEnable your putting the : in the code. You dont need to do that, they are added automatically.

    Change this:
    Code:
    this.getConfig().addDefault("Egg_explosion_radius:", 2);
    To this:
    Code:
    this.getConfig().addDefault("Egg_explosion_radius", 2);
     
  11. Offline

    vYN

    hmm ok. that removed those... but i'm still getting the same error :/

    I moved the int out from the PlayerEggThrowEvent. And added it right under "public class Explode implemets Listener{"

    like this: public static int exrad = plugin.getConfig().getInt("Egg_explosion_radius");

    but then again. a new error :/

    Code:
    44%
    2012-01-22 06:32:31 [INFO] [ExplodingEggs] Loading ExplodingEggs v1.2.
    2012-01-22 06:32:31 [INFO] ExplodingEggs Author(s): +[GudfareN]
    2012-01-22 06:32:31 [SEVERE] Error occurred while enabling ExplodingEggs v1.2 (Is it up to date?): null
    java.lang.ExceptionInInitializerError
        at me.GudfareN.ExplodingEggs.Main.onEnable(Main.java:21)
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:231)
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:1059)
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:253)
        at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:191)
        at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:174)
        at net.minecraft.server.MinecraftServer.t(MinecraftServer.java:356)
        at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:343)
        at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:175)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:407)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
    Caused by: java.lang.NullPointerException
        at me.GudfareN.ExplodingEggs.Explode.<clinit>(Explode.java:12)
        ... 11 more
    fixed that. Back to the first error again...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
Thread Status:
Not open for further replies.

Share This Page