Solved NullPointerException passing plugin as argument

Discussion in 'Plugin Help/Development/Requests' started by 2008Choco, Sep 20, 2015.

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

    2008Choco

    I've been trying to figure out this issue for the past little bit with many headaches and very little patience. I know the cause of it I think, I just don't quite know how to fix it. I have a couple of classes that need the parameters of "plugin" (being the main class, obviously). Unfortunately, they're not like the InteractWithBlock event where you can just pass this as a parameter in the onEnable method and use it as the variable "plugin". They're just random classes that can only be accessed through the InteractWithBlock event class. For whatever reason that I can't explain, it seems to be null.

    I've tried everything to fix it from declaring the class in the main class and using plugin.variable.method (ex), and declaring plugin in the constructor of the classes, but I just can't get it to work. Could I get a bit of help?

    Here's the error log in case I'm wrong on the NullPointer error:
    http://pastebin.com/WMhBXjb3

    EDIT: Here is an example of what I'm attempting to do. This is the constructor for my InteractWithBlock class:
    http://pastebin.com/8zRP64aU
    It receives the plugin parameter from the onEnable method in the main class, as an instance of "this". So it's definitely not null. I'm probably doing something wrong
     
    Last edited: Sep 20, 2015
  2. Offline

    KoleDogCodes

    This is how i pass my classes around.

    Test.java
    static MainClass plugin;
    public Test(MainClass i){
    plugin = i;
    }

    MainClass.java
    Then in your onEnable(){
    new Test (this);
    }
     
  3. Offline

    timtower Moderator Moderator

  4. Offline

    KoleDogCodes

    How so? Is it because I can accecs the plugin variable from any class?

    If you can't use static just use a different modifier. Like public or private but I usr static cause I like to accecs my plugin variable from any class :)
     
  5. Offline

    timtower Moderator Moderator

    @KoleDogCodes Use constructors and private normal fields for that. There is no need for static in plugins.
     
  6. Offline

    2008Choco

    @KoleDogCodes @timtower
    Thanks guys, but I found out that the passing of the plugin arguments wasn't causing the issue. I was referring to a file before the file was actually being loaded into memory. That was causing the NullPointer. I simply moved the line of code accessing the file in to a method. ^-^ All good now
     
Thread Status:
Not open for further replies.

Share This Page