Getting variables from another class?

Discussion in 'Plugin Development' started by Nytewarrior, Feb 29, 2012.

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

    Nytewarrior

    Hi, I have spent quite a while attempting to get a config from a Listener, but that would always give me an error related to a NullPointerException. I figured out that it happened when I tried to get any variable from the Listener. I know someone is going to say it's a nullpointerexeption because I have no constructor, but I have this in my Listener:

    Code:
     public static AntiGreif plugin;
       
    public MyBlockListener(AntiGreif instance) {
            plugin = instance; }
    Please help, and say if you need to see more code.
     
  2. Offline

    Zaros

    If your trying to get a variable, just use a getter and setter.
     
  3. Or paste the problematic code here. Then we can have a look at it.
     
  4. Using getters and setters or public vars are usefull for multi-instanced classes, like Player class, that has multiple instances... but for your main plugin class which should always have one instance, you can use static variables.

    You just set them as "public static" in the main class and then use the class's name to use them in other classes, like MyPlugin.someVariable.

    Also, find WHERE you get the null error, because it could be something else wrong... you could post your entire class so we can have a look like dademon said.
     
  5. Offline

    Nytewarrior

    I looked at the line numbers, and it's always on the line i'm trying to get the variable. Also, I thought you couldn't use static for configs?
     
  6. You can use but you must do something special for them, I don't remember what, it's in the wiki.
    Still, I don't actually know what your code does because you didn't post the code where you get the error.
     
  7. Offline

    Nytewarrior

    Show Spoiler

    Code:
    2012-02-28 18:31:41 [INFO] This server is running CraftBukkit version git-Bukkit-1.1-R4-b1938jnks (MC: 1.1) (Implementing API version 1.1-R4)
    2012-02-28 18:31:41 [SEVERE] Could not load 'plugins\AntiGreifer.jar' in folder 'plugins': 
    java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:136)
    at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:286)
    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:201)
    at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:164)
    at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:140)
    at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigurationManager.java:52)
    at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:148)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:408)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
    Caused by: java.lang.NullPointerException
    at me.nytewarrior.AntiGreif.MyBlockListener.<init>(MyBlockListener.java:18)
    at me.nytewarrior.AntiGreif.AntiGreif.<init>(AntiGreif.java:24)
    ... 13 more 

    Pretty sure it's this, Sorry for the delay, was pretty busy today.
     
  8. Offline

    acuddlyheadcrab

    Ah the InvocationTargetException... Always f-ed me over.

    Well it would help more to show the class file, or at least the couple lines that aren't working (0-18 or 18-24)...?



    I use static for many of my plugins that use configs. I probably use different config methods than you, but for the most part, you can access a static FileConfiguration once it has been initialized in your onEnable() method.
     
Thread Status:
Not open for further replies.

Share This Page