Solved IllegalArgumentException: Plugin cannot be null - I'm giving up

Discussion in 'Plugin Development' started by rsod, Aug 15, 2013.

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

    rsod

    Code:
    10:37:20 [SEVERE] Could not pass event PlayerInteractEvent to Modifications v0.1
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
            at org.bukkit.plugin.TimedRegisteredListener.callEvent(TimedRegisteredListener.java:30)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:478)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:463)
            at org.bukkit.craftbukkit.v1_6_R2.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:207)
            at org.bukkit.craftbukkit.v1_6_R2.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:177)
            at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java:608)
            at net.minecraft.server.v1_6_R2.Packet15Place.handle(SourceFile:58)
            at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:230)
            at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java:116)
            at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
            at org.spigotmc.netty.NettyServerConnection.b(NettyServerConnection.java:125)
            at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:592)
            at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:239)
            at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:481)
            at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:413)
            at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:192)
            at org.bukkit.craftbukkit.v1_6_R2.scheduler.CraftScheduler.validate(CraftScheduler.java:391)
            at org.bukkit.craftbukkit.v1_6_R2.scheduler.CraftScheduler.runTaskTimer(CraftScheduler.java:120)
            at org.bukkit.craftbukkit.v1_6_R2.scheduler.CraftScheduler.runTaskLater(CraftScheduler.java:104)
            at org.rsod.Modifications.ItemBlastGrenade.onInteract(ItemBlastGrenade.java:127)
            at sun.reflect.GeneratedMethodAccessor860.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:601)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
            ... 17 more
    
    Code:java
    1. Bukkit.getScheduler().runTaskLater((Modifications) Bukkit.getPluginManager().getPlugin("Modifications"), new ItemBlastGrenadeTask1(p), 10);
     
  2. Offline

    tommycake50

    That is a bad way to do it.
    pass an instance of the main class through the constructor of that class and save it in a variable called instance or something.
     
    Axe2760 likes this.
  3. Offline

    Axe2760

    From what I see, that means is that
    Code:
    Bukkit.getPluginManager().getPlugin("Modifications")
    
    Is null. There is no plugin called "Modifications". Check the name you used in your plugin.yml?

    Edit: Do what tommycake said, it's much easier and will probably be less frustrating.
     
    tommycake50 likes this.
  4. Offline

    rsod

    tommycake50 I originally used constructor, but it refused to work at all, even load plugin.
    Code:
    2013-08-15 10:13:25 [INFO] [Modifications] Enabling Modifications v0.1
    2013-08-15 10:13:25 [SEVERE] Error occurred while enabling Modifications v0.1 (Is it up to date?)
    java.lang.NoSuchMethodError: org.rsod.Modifications.ItemBlastGrenade.<init>(Lorg/rsod/Modifications/Modifications;)V
        at org.rsod.Modifications.Modifications.onEnable(Modifications.java:58)
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:382)
        at net.diamondmine.reloader.PluginReloader.loadPlugin(PluginReloader.java:216)
        at net.diamondmine.reloader.PluginReloader.reloadPlugin(PluginReloader.java:252)
        at net.diamondmine.reloader.PluginReloader.onCommand(PluginReloader.java:89)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServer.java:527)
        at net.minecraft.server.v1_6_R2.PlayerConnection.handleCommand(PlayerConnection.java:979)
        at net.minecraft.server.v1_6_R2.PlayerConnection.chat(PlayerConnection.java:890)
        at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java:837)
        at net.minecraft.server.v1_6_R2.Packet3Chat.handle(SourceFile:49)
        at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:230)
        at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java:116)
        at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
        at org.spigotmc.netty.NettyServerConnection.b(NettyServerConnection.java:125)
        at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:592)
        at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:239)
        at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:481)
        at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:413)
        at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    
    Axe2760
    in another class in same package same method works just fine. As well as constructor way works fine with another classes. Only this won't work...
     
  5. Offline

    tommycake50

    Can i see the constructor?
     
  6. Offline

    rsod

    tommycake50
    Code:java
    1. public class ItemBlastGrenade implements Listener {
    2. private Modifications mplugin;
    3.  
    4. public ItemBlastGrenade(Modifications plugin){
    5. this.mplugin = plugin;
    6. }
    7. //...

    onEnable method:
    Code:java
    1. @Override
    2. public void onEnable(){
    3. Bukkit.getPluginManager().registerEvents(new Listeners(), this);
    4. Bukkit.getPluginManager().registerEvents(new ExplosiveBow(this), this);
    5. Bukkit.getPluginManager().registerEvents(new TeleportationBow(this), this);
    6. Bukkit.getPluginManager().registerEvents(new ItemBlastGrenade(this), this);
    7. if(Bukkit.getPluginManager().getPlugin("NoCheatPlus") != null){
    8. Exempter.use = true;
    9. }
    10. this.getLogger().info("Enabled! test!");
    11. }

    TeleportationBow and ExplosiveBow has THE SAME constructors and they are working correctly.
     
  7. Offline

    tommycake50

    Also why are you using Bukkit and not getServer()?
     
  8. Offline

    rsod

    tommycake50 hmm, would it change something? It doesn't changing anything anyways.

    Code:java
    1. @Override
    2. public void onEnable(){
    3. this.getServer().getPluginManager().registerEvents(new Listeners(), this);
    4. this.getServer().getPluginManager().registerEvents(new ExplosiveBow(this), this);
    5. this.getServer().getPluginManager().registerEvents(new TeleportationBow(this), this);
    6. this.getServer().getPluginManager().registerEvents(new ItemBlastGrenade(this), this);
    7. if(this.getServer().getPluginManager().getPlugin("NoCheatPlus") != null){
    8. Exempter.use = true;
    9. }
    10. this.getLogger().info("Enabled! test!");
    11. }

    I have an idea what can I do, I'll try it and post if it will help or not.. but it will require more time.
     
  9. Offline

    desht

    rsod there's no practical difference between Bukkit.getPluginManager() and this.getServer().getPluginManager() - they get you the same PluginManager object. So not relevant.

    Your stack trace shows a NoSuchMethod error for the ItemBlastGrenade constructor which takes a single argument of type Modifications. The code you posted looks OK (you do indeed have such a constructor), which leads me to believe that the ItemBlastGrenade.class file in your JAR is possibly out of date (i.e. there's a compiled version in your JAR which is lacking that constructor definition). Clean out your build environment and rebuild the plugin JAR from scratch and see if you still get the error.
     
  10. Offline

    rsod

    Ok...
    I deleted project from eclipse, then created it again, pasted all source codes...
    and it works now...
    wtf...
     
  11. Offline

    desht

    Yeah... stale class file in your JAR. Eclipse's build environment is somewhat ... sub-optimal. I switched to using Maven a long time ago, and haven't looked back :)
     
  12. Offline

    tommycake50

    But if you import Bukkit you're wasting ram >:C
     
  13. Offline

    rsod

    emm? How? It's kinda offtopic, but I'd like to know how.
     
  14. Offline

    tommycake50

    Because when you import a class you're telling the JRE to load it into ram for your class to access it works almost like the preprocessor directives in C or C++.
    Thats why its bad say if your using a JFrame to import javax.swing.* because you are loading the ENTIRE Swing library into ram.
     
  15. Offline

    desht

    I'm calling BS on this one.

    Importing classes at compile-time has exactly zero effect on run-time RAM usage.

    If you're concerned about the 10KB Bukkit.class file having an effect on compile-time memory usage, then seriously - get a computer that was built this century.

    If you're concerned about the 10KB Bukkit.class file having an effect on run-time memory usage, I'll refer you to the previous comment, plus: it's almost certainly already loaded into the JVM already, since multiple Bukkit classes use it. If you have any plugin that uses permissions, the scheduler, or handles an ItemStack, then it's loaded. If you've used any builtin commands, then it's loaded.

    Oh, and Java import works nothing like the C/C++ preprocessor.
     
  16. Offline

    tommycake50

    right. good to know.
     
  17. Axe2760
    Assist
    timtower
    Sorry to bump this one up, but I am having the same problem, and this is not solving it. Here is my code:


    Code:java
    1. package me.HeyAwesomePeople.warframe;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.entity.Entity;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.util.Vector;
    8.  
    9. public abstract class Methods{
    10.  
    11. static Main plugin;
    12.  
    13. public Methods(Main instance) {
    14. plugin = instance;
    15. }
    16. public static void mag1(Player player) {
    17. if (player.getLevel() >= 25) {
    18. /*
    19. * TODO Pull the player on your cursor, bringing him into melee
    20. * range!
    21. */
    22. player.setLevel(player.getLevel() - 25);
    23. } else {
    24. player.sendMessage(ChatColor.RED
    25. + "You do not have enough XP levels to perform this ability!");
    26. }
    27. player.sendMessage(ChatColor.RED + "Mag's 1st power works!");
    28. }
    29.  
    30. public static void mag2(Player player) {
    31. if (player.getLevel() >= 100) {
    32. for (final Entity entity : player.getNearbyEntities(6.0D, 6.0D, 6.0D)) {
    33. if (entity instanceof Player) {
    34. final Player nearby = (Player) entity;
    35. nearby.setVelocity(new Vector(0, 4, 0));
    36. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    37. public void run(){
    38. entity.getWorld().createExplosion(nearby.getLocation(), 1);
    39. }
    40. }, 20L); //replace the 3L to whatever delay you need
    41. }
    42. }
    43. player.setLevel(player.getLevel() - 100);// Take 100 XP
    44. } else {
    45. player.sendMessage(ChatColor.RED
    46. + "You do not have enough XP levels to perform this ability!");
    47. }
    48. }
    49. }
    50.  

    And my main class(Only part you would need):
    Code:java
    1. public class Main extends JavaPlugin {
    2. public final Logger logger = Logger.getLogger("Minecraft");
    3. public static Main plugin;
    4. public final InteractListener1 bl = new InteractListener1(this);
    5. // Lists
    6. List<String> magUsers = new ArrayList<String>();
    7. List<String> usedPlayCommand = new ArrayList<String>();
    8.  
    9. @Override
    10. public void onEnable() {
    11. PluginManager pm = getServer().getPluginManager();
    12. pm.registerEvents(this.bl, this);
    13. PluginDescriptionFile pdffile = this.getDescription();
    14. this.logger.info(pdffile.getName() + " Has been enabled!");
    15.  
    16. getConfig().options().copyDefaults(false);
    17. File file = new File("plugins" + File.separator + "MCWarframe"
    18. + File.separator + "config.yml");
    19. if (!file.exists()) {
    20. this.logger.info(pdffile.getName() + " - " + "INFO" + " - "
    21. + "Config file not found! Creating...");
    22. FileConfiguration config = getConfig();
    23. // Create default Lobby
    24. config.set("Lobby." + "x", 0);
    25. config.set("Lobby." + "y", 70);
    26. config.set("Lobby." + "z", 0);
    27. // Create default World
    28. config.set("world", "world");
    29. this.logger.info(pdffile.getName() + " - " + "INFO" + " - "
    30. + "Config file successfully created!");
    31. }
    32. saveConfig();
    33. }
    34.  
    35. @Override
    36. public void onDisable() {
    37. PluginDescriptionFile pdffile = this.getDescription();
    38. this.logger.info(pdffile.getName() + " Has been disabled!");
    39. }

    Whenever I try to perform mag2() it throws a plugin cannot be null error on
    Code:java
    1. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
     
  18. Offline

    Axe2760

    HeyAwesomePeople How are you calling it? For example,

    Code:
    public class Main extends JavaPlugin{
      //creating an instance of your methods class
      public Methods m;
      @Override
      public void onEnable(){
        //passes a valid instance of your plugin to the constructor
        m = new Methods(this);
        //run mag2
        m.mag2(\*player*\);
      }
    }
    
    Apply that to your main class.
     
    HeyAwesomePeople likes this.
  19. Axe2760
    The mag2() method is being designed to throw a player in the air, then make them explode while in the air. So its being called actually on right click event with a stick.
     
  20. Offline

    Axe2760

    HeyAwesomePeople likes this.
  21. Axe2760
    Calling it? All I use to get mag2 is:
    Code:java
    1. Methods.mag2(player);

    Nothing else.
     
  22. Offline

    Axe2760

    HeyAwesomePeople Thats your problem. Without the constructor, plugin is still null. Inside the constructor your setting your plugin variable to instance. Well, take that bit off, and the only thing that defines plugin is
    Code:
    public Main plugin;
    Which isn't instantiated, and returns null until you assign it something.

    By doing Methods.mag2, you're leaving out the constructor. Instead, make an instance of your class methods.
    Code:
    Methods methods = new Methods(this);
    
    If you're calling it from another class, repeat the process, per se.
    Code:
    public class MyListener implements Listener{
      private Main plugin;
      private Methods methods;
      public MyListener(Main plugin){
        this.plugin = plugin;
        this.methods = new Methods(plugin);
      }
      @EventHandler
      public void blabla(blabla){
        methods.mag2(event.getPlayer());
      }
    }
    
    Code:
    public class DerpPlugin extends JavaPlugin{
      @Override
      public void onEnable(){
        this.getServer().getPluginManager().registerEvents(new MyListener(this), this); //notice the constructor
      }
    }
     
    HeyAwesomePeople likes this.
  23. Offline

    Sabersamus

    I think (I could be wrong about this), but, You have mag2 as static, and because of this it's possible that plugin is not set. try making mag2 non-static, and using an object of the class when using mag2

    Edit: Ninja'd :p
     
    Axe2760 likes this.
  24. Axe2760
    I was told this was simple.
    Where do I place that?
    So this in my InteractListener1? Now this:
    this.methods = new Methods(plugin);
    Error: Cannot instantiate the type Methods
    And private Methods methods is unused.
    I already do this in my Main class. Look at the code.
     
  25. Offline

    Axe2760

    HeyAwesomePeople Derp, I didn't realize your class was abstract/you can't instantitiate it. I'd honestly just make it not abstract, not sure. As for doing it in the main class, I was referring to passing an instance of the plugin to both classes, not registering events.
     
    HeyAwesomePeople likes this.
  26. Axe2760
    Okay. I forgot it was abstract. Removed. Everything else imported as you said.
    EDIT: OH MY GOSH. THANK YOU. I HAVE BEEN WORKING ON THIS FOR HOURS!
    THANK YOU.
     
  27. Offline

    Axe2760

    HeyAwesomePeople likes this.
Thread Status:
Not open for further replies.

Share This Page