Solved Plugin cannot be null?

Discussion in 'Plugin Development' started by valon750, Feb 12, 2014.

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

    valon750

    Hey all,

    So I'm finally getting round to a much needed polish of my FlightPack plugin, but I can't for the life of me figure out what causes this "plugin cannot be null" error.

    Code:
        // Charging Station Place
        @EventHandler
        public void onDropperPlace(BlockPlaceEvent event){
     
            final Block block = event.getBlock();
            Player player = event.getPlayer();
            ItemStack item = player.getItemInHand();
     
            if (Jetpack.isStation(item)){
     
                Bukkit.getScheduler().runTaskLater(this.plugin, new Runnable(){
     
                    @SuppressWarnings("deprecation")
                    public void run(){                   
                        block.setData((byte)1);
     
                        String world = block.getWorld().getName().toString();
                        Integer x = block.getLocation().getBlockX();
                        Integer y = block.getLocation().getBlockY();
                        Integer z = block.getLocation().getBlockZ();
     
                        String location = world + ">" + x.toString() + ">" + y.toString() + ">" + z.toString();
     
                        File station = new File (plugin.getDataFolder() + File.separator + "Charging Stations", "Charging Stations");
                        FileConfiguration stationconfig;
                        stationconfig = YamlConfiguration.loadConfiguration(station);
     
                        if (!station.exists()){
                            try {
                                station.createNewFile();
                            } catch (IOException e1) {
                                return;
                            }
                        } else {
     
                            stationconfig.set("Locations." + location + ".World", block.getWorld().getName().toString());
                            stationconfig.set("Locations." + location + ".X", block.getX());
                            stationconfig.set("Locations." + location + ".Y", block.getY());
                            stationconfig.set("Locations." + location + ".Z", block.getZ());
     
                            try {
                                stationconfig.save(station);
                            } catch (IOException e) {
                                return;
                            }
                        }
                    }
                }, 1L);
            }
        }
    I know how you guys appreciate code snippets!

    Basically it's firing on the line initiating a new file, now the issue is that for one, no errors show in eclipse itself, and I do in fact have "plugin" stated at the top, that being "public FlightPack plugin;".

    Now, I have a different method than normal to registering listeners, therefore I'm unable to simply do..

    "public classfile (FlightPack instance){
    plugin = instance;
    }"

    Unless of course you guys know a way I can incorporate it in to my registerListeners method?

    "this.registerListeners(new EventListener(), this);"

    Code:
        private void registerListeners(Listener... listeners) {
            for (Listener listener : listeners) {
                this.getServer().getPluginManager().registerEvents(listener, this);
            }
        }
    Anyway, rather out of the loop coding wise, with me having about 50 other things going on, it's had to take a back seat, and therefore the ol' brains gone a bit thick.

    Thanks in advance everyone!
     
  2. Offline

    xTigerRebornx

    valon750 You can make a static reference to your plugin
    In your class that extends JavaPlugin.
    Code:
    public static ClassName yourVariable;
     
    public void onEnable(){
    yourVariable = this;
    // Rest of your code
    }
    public void onDisable(){
    yourVariable = null; // Prevent memory leaks
    }
    You could do this, or simply not use that method for the one listener (BTW Code not tested) and register it normally. BTW, I <3 Flightpack.
    Edit: Could also just try getting the plugin using Bukkit (I believe its Bukkit.getPluginManager().getPlugin("Name");)
     
  3. Offline

    Garris0n

    Just pass "this" into the declaration of EventListener while registering...

    Also, #BetterJetpacksMasterRace
    #BetterJetpacksHasGoneSoLongWithoutUpdatesItBarelyHasAnyFeaturesIShouldFixThat...
     
  4. Offline

    valon750

    xTigerRebornx Garris0n

    Just so we're clear, I'm adding "publicstatic FlightPack plugin;" to the main class, and "plugin = this;" in onEnable.

    Now, I was under the impression that static would allow me to access it from any other class, however that doesn't seem to be the case, and all instances of plugin in other classes don't appear to be resolved.
     
  5. Offline

    1Rogue


    Don't use static, and especially don't use a public-static non-final variable.

    You want your code to be as minimally-exposed as possible. For this, you should pass your main class instance via constructor:

    Code:java
    1. public class YourMainClass extends JavaPlugin {
    2.  
    3. private SomeManager manager;
    4.  
    5. public void onEnable() {
    6. this.manager = new SomeManager(this);
    7. }
    8.  
    9. }


    Code:java
    1. public class SomeManager {
    2.  
    3. private final YourMainClass plugin;
    4.  
    5. public SomeManager(YourMainClass plugin) {
    6. this.plugin = plugin;
    7. }
    8.  
    9. }
     
    Garris0n likes this.
  6. Offline

    valon750

    1Rogue

    Okay, so I set it up this way, yet now it's complaining about not being able to make a static reference to a non-static field.
     
  7. Offline

    Garris0n

    Don't use static methods either.
     
  8. Offline

    valon750

    Garris0n

    Okay I've took a wrong turn somewhere xD

    Basically the method which was previously static is currently called on a repeating scheduler, which is in my main class, the method I need running is in a separate class.

    "Cannot make a static reference to the non-static method HandleFuel() from the type FuelManager"

    So I'm not too sure what to do at this point.
     
  9. Offline

    Garris0n

    You need to get an instance of the FuelManager class (just like how you did with the main class) instead of using static for everything. Also, as dictated by these naming conventions, you should not have a method called HandleFuel().
     
  10. Offline

    valon750

    Garris0n 1Rogue

    Thank you everyone for the help so far,

    However I seem to be hitting a wall, I add..

    Code:
        private final YourMainClass plugin;
     
        public SomeManager(YourMainClass plugin) {
            this.plugin = plugin;
        }
    .. to the correct places, yet when referencing to for example, the fuel manager class, and attempt to use the handleFuel method, I'm prompted to change the method handleFuel from "public void" to "public static void".

    This is the program telling me to do this, despite others saying not to use static.

    This being the timer I utilise in my main class, which should be causing the handleFuel method to fire.

    Code:
            this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
     
                public void run() {
                    FuelManager.handleFuel();
                }
     
            }, 0L, 20L);
     
  11. Offline

    1Rogue

    It's suggesting you change it because you are attempting to access it in a static way. You would reference it via the instance of the class, not the class itself:

    Code:java
    1. private final YourMainClass plugin;
    2.  
    3. public SomeManager(YourMainClass plugin) {
    4. this.plugin = plugin;
    5. }
    6.  
    7. public void someMethod() {
    8. this.plugin.handleFuel();
    9. }
     
  12. Offline

    valon750

    1Rogue

    But what I'm going for is that the method I need to use is in its own class, and I need it called from the main class.

    This from the looks of it is insinuating that the method is in fact in the main class.
     
  13. Offline

    Munnzeh

    I had a similar problem and i still can't fix it after 2 weeks of trying :(
     
  14. Offline

    1Rogue


    So keep a reference to that class from the main class, and then call the method within it from there?
     
Thread Status:
Not open for further replies.

Share This Page