Plugin cannot be null

Discussion in 'Plugin Development' started by chasertw123, Mar 23, 2014.

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

    chasertw123

    So I am having a problem that I can't seem to figure out. When a player executes a certain command it calls a method I made but that method is causing this error. I can't quite figure out the problem is so I thought I would ask the bukkit community. Here is the error:

    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'repair' in plugin CrownPvP v1.0
       at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Craftbukkit-1335]
       at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:175) ~[craftbukkit.jar:git-Craftbukkit-1335]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:706) ~[craftbukkit.jar:git-Craftbukkit-1335]
        at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:984) [craftbukkit.jar:git-Craftbukkit-1335]
        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:829) [craftbukkit.jar:git-Craftbukkit-1335]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Craftbukkit-1335]
        at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:65) [craftbukkit.jar:git-Craftbukkit-1335]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:147) [craftbukkit.jar:git-Craftbukkit-1335]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(ServerConnection.java:77) [craftbukkit.jar:git-Craftbukkit-1335]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:700) [craftbukkit.jar:git-Craftbukkit-1335]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:273) [craftbukkit.jar:git-Craftbukkit-1335]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:562) [craftbukkit.jar:git-Craftbukkit-1335]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Craftbukkit-1335]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Craftbukkit-1335]
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[craftbukkit.jar:git-Craftbukkit-1335]
        at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.validate(CraftScheduler.java:391) ~[craftbukkit.jar:git-Craftbukkit-1335]
        at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.runTaskTimer(CraftScheduler.java:120) ~[craftbukkit.jar:git-Craftbukkit-1335]
        at org.bukkit.scheduler.BukkitRunnable.runTaskTimer(BukkitRunnable.java:100) ~[craftbukkit.jar:git-Craftbukkit-1335]
        at me.chasertw123.kitpvp.misc.Repair.Rep(Repair.java:44) ~[?:?]
        at me.chasertw123.kitpvp.KitPvP.onCommand(KitPvP.java:101) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Craftbukkit-1335]
        ... 13 more
    Here is my code from the Repair.java and the KitPvP.java and another thing that might be casuing it to happen:

    KitPvP.java:
    Code:java
    1. package me.chasertw123.kitpvp;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.logging.Logger;
    6.  
    7. import me.chasertw123.kitpvp.listeners.DoubleJumpListener;
    8. import me.chasertw123.kitpvp.listeners.PlayerListener;
    9. import me.chasertw123.kitpvp.listeners.SoupListener;
    10. import me.chasertw123.kitpvp.misc.ListsRemover;
    11. import me.chasertw123.kitpvp.misc.Messages;
    12. import me.chasertw123.kitpvp.misc.Repair;
    13. import me.chasertw123.kitpvp.misc.Respawn;
    14. import me.chasertw123.kitpvp.misc.Soup;
    15.  
    16. import org.bukkit.Bukkit;
    17. import org.bukkit.ChatColor;
    18. import org.bukkit.command.Command;
    19. import org.bukkit.command.CommandSender;
    20. import org.bukkit.entity.Player;
    21. import org.bukkit.inventory.ItemStack;
    22. import org.bukkit.plugin.PluginManager;
    23. import org.bukkit.plugin.java.JavaPlugin;
    24. import org.bukkit.potion.PotionEffect;
    25.  
    26. public class KitPvP extends JavaPlugin {
    27.  
    28. /** Has Kit Checker **/
    29. public static ArrayList<String> haskit = new ArrayList<String>();
    30.  
    31. /** Repair/Refill Array Lists **/
    32. public static ArrayList<String> refill = new ArrayList<String>();
    33. public static ArrayList<String> repair = new ArrayList<String>();
    34.  
    35. /** Free Player Kit Array Lists **/
    36. public static ArrayList<String> barbarian = new ArrayList<String>();
    37. public static ArrayList<String> pyro = new ArrayList<String>();
    38. public static ArrayList<String> archer = new ArrayList<String>();
    39. public static ArrayList<String> snowman = new ArrayList<String>();
    40. public static ArrayList<String> ninja = new ArrayList<String>();
    41.  
    42. /** Premium Player Kit Array List **/
    43. public static ArrayList<String> dragon = new ArrayList<String>();
    44. public static ArrayList<String> irongolem = new ArrayList<String>();
    45.  
    46. /** Inventory Saver HashMaps **/
    47. public static HashMap<String, ItemStack[]> invsave = new HashMap<String, ItemStack[]>();
    48. public static HashMap<String, ItemStack[]> armorsave = new HashMap<String, ItemStack[]>();
    49.  
    50. public static KitPvP plugin;
    51.  
    52. Logger logger = Logger.getLogger("Minecraft");
    53.  
    54. /** Enabled **/
    55. public void onEnable() {
    56. this.getConfig().options().copyDefaults(true);
    57. this.saveConfig();
    58. registerEvents();
    59. logger.info("[CrownPvP] has been enabled!");
    60. }
    61.  
    62. /** Disable **/
    63. public void onDisable() {
    64. logger.info("[CrownPvP] has been disabled!");
    65. }
    66.  
    67. /** Event Register **/
    68. public void registerEvents() {
    69. PluginManager pm = this.getServer().getPluginManager();
    70. pm.registerEvents(new KitSelector(this), this);
    71. pm.registerEvents(new Special(this), this);
    72. pm.registerEvents(new PlayerListener(this), this);
    73. pm.registerEvents(new DoubleJumpListener(this), this);
    74. pm.registerEvents(new SoupListener(this), this);
    75. pm.registerEvents(new Respawn(this), this);
    76. }
    77.  
    78. /** Commands **/
    79. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    80. if (!(sender instanceof Player))
    81. return true;
    82.  
    83. Player p = (Player) sender;
    84.  
    85. if (cmd.getName().equalsIgnoreCase("Selectors")) {
    86.  
    87. if (p.hasPermission("crownpvp.selection")) {
    88. p.getInventory().clear();
    89. p.getInventory().setArmorContents(null);
    90. for (PotionEffect effect : p.getActivePotionEffects()) { p.removePotionEffect(effect.getType()); }
    91. p.getInventory().addItem(SelectorItemStacks.Selector());
    92. p.getInventory().addItem(SelectorItemStacks.DonatorSelector());
    93. return true;
    94. } else {
    95. Messages.noPermission(p);
    96. return true;
    97. }
    98.  
    99. } else if (cmd.getName().equalsIgnoreCase("Repair")) {
    100. if (p.hasPermission("crownpvp.repair")) {
    101. Repair.Rep(p);
    102. } else {
    103. Messages.noPermission(p);
    104. return true;
    105. }
    106.  
    107. } else if (cmd.getName().equalsIgnoreCase("Refill")) {
    108. if (p.hasPermission("crownpvp.refill")) {
    109. Soup.RefillSoup(p);
    110. return true;
    111. } else {
    112. Messages.noPermission(p);
    113. return true;
    114. }
    115. } else if (cmd.getName().equalsIgnoreCase("Reset")) {
    116. if (p.hasPermission("crownpvp.reset")) {
    117. if (args.length == 1) {
    118. if (p.hasPermission("crownpvp.reset.others")) {
    119.  
    120. Player target = Bukkit.getPlayerExact(args[0]);
    121.  
    122. if (target != null) {
    123. ListsRemover.Remove(p);
    124. target.getInventory().clear();
    125. target.getInventory().setArmorContents(null);
    126. for (PotionEffect effect : target.getActivePotionEffects()) { target.removePotionEffect(effect.getType()); }
    127. target.setExp(0F);
    128. target.setFireTicks(0);
    129. target.setHealth(p.getMaxHealth());
    130. target.getInventory().addItem(SelectorItemStacks.Selector());
    131. target.getInventory().addItem(SelectorItemStacks.DonatorSelector());
    132. Respawn.Spawn(target);
    133. Messages.sendMessage(p, ChatColor.GREEN + "You reset the kit status of "
    134. + ChatColor.GOLD + target.getName() + ChatColor.GREEN + "!");
    135. Messages.sendMessage(target, ChatColor.GOLD + p.getName() + ChatColor.GREEN
    136. + " reset your kit status!");
    137.  
    138. } else Messages.notFound(p);
    139.  
    140. } else Messages.noPermission(p);
    141.  
    142. } else if (args.length == 0) {
    143. ListsRemover.Remove(p);
    144. p.getInventory().clear();
    145. p.getInventory().setArmorContents(null);
    146. for (PotionEffect effect : p.getActivePotionEffects()) { p.removePotionEffect(effect.getType()); }
    147. p.setExp(0F);
    148. p.setFireTicks(0);
    149. p.setHealth(p.getMaxHealth());
    150. p.getInventory().addItem(SelectorItemStacks.Selector());
    151. p.getInventory().addItem(SelectorItemStacks.DonatorSelector());
    152. Respawn.Spawn(p);
    153. Messages.sendMessage(p, ChatColor.GREEN + "You reset your kit status!");
    154. }
    155. }
    156. } else if (cmd.getName().equalsIgnoreCase("SetRespawn")) {
    157. if (p.hasPermission("crownpvp.setrespawn")) {
    158. Respawn.setRespawn(p);
    159. } else Messages.noPermission(p);
    160. }
    161. return false;
    162. }
    163. }


    Repair.java:
    Code:java
    1. package me.chasertw123.kitpvp.misc;
    2.  
    3. import me.chasertw123.kitpvp.KitPvP;
    4. import me.chasertw123.kitpvp.kits.ArcherKit;
    5. import me.chasertw123.kitpvp.kits.BarbarianKit;
    6. import me.chasertw123.kitpvp.kits.DragonKit;
    7. import me.chasertw123.kitpvp.kits.IrongolemKit;
    8. import me.chasertw123.kitpvp.kits.NinjaKit;
    9. import me.chasertw123.kitpvp.kits.PyroKit;
    10. import me.chasertw123.kitpvp.kits.SnowmanKit;
    11. import me.chasertw123.kitpvp.timers.xpCoolDown;
    12.  
    13. import org.bukkit.ChatColor;
    14. import org.bukkit.Material;
    15. import org.bukkit.entity.Player;
    16. import org.bukkit.inventory.ItemStack;
    17. import org.bukkit.inventory.meta.ItemMeta;
    18. import org.bukkit.potion.PotionEffect;
    19. import org.bukkit.potion.PotionEffectType;
    20.  
    21. public class Repair {
    22.  
    23. private static KitPvP plugin;
    24.  
    25. public Repair(KitPvP plugin) {Repair.plugin = plugin;}
    26.  
    27. public static void Rep(Player p) {
    28. if (KitPvP.haskit.contains(p.getName())) {
    29. if (KitPvP.refill.contains(p.getName())) {
    30. Messages.sendMessage(p, ChatColor.RED + "You can not use that command until you are done refilling your soups!");
    31. return;
    32. } else if (KitPvP.repair.contains(p.getName())) {
    33. Messages.sendMessage(p, ChatColor.RED + "You can not use that command until you are done repairing all your materials!");
    34. return;
    35. } else {
    36. PotionEffect blind = new PotionEffect(PotionEffectType.BLINDNESS, 110, 5);
    37.  
    38. KitPvP.repair.add(p.getName());
    39. p.getInventory().clear();
    40. p.getInventory().setArmorContents(null);
    41. p.getInventory().setHelmet(wool());
    42. p.addPotionEffect(blind);
    43.  
    44. new xpCoolDown(p, 10).runTaskTimer(plugin, 20L, 20L);
    45.  
    46. p.getInventory().setHelmet(null);
    47.  
    48. if (KitPvP.barbarian.contains(p.getName())) {
    49. BarbarianKit.Barbarian(p);
    50. } else if (KitPvP.pyro.contains(p.getName())) {
    51. PyroKit.Pyro(p);
    52. } else if (KitPvP.archer.contains(p.getName())) {
    53. ArcherKit.Archer(p);
    54. } else if (KitPvP.snowman.contains(p.getName())) {
    55. SnowmanKit.Snowman(p);
    56. } else if (KitPvP.ninja.contains(p.getName())) {
    57. NinjaKit.Ninja(p);
    58. } else if (KitPvP.dragon.contains(p.getName())) {
    59. DragonKit.Dragon(p);
    60. } else if (KitPvP.irongolem.contains(p.getName())) {
    61. IrongolemKit.Irongolem(p);
    62. }
    63.  
    64. KitPvP.repair.remove(p.getName());
    65. Messages.sendMessage(p, ChatColor.GREEN + "All of your armor and soups were replaced!");
    66. }
    67. } else Messages.sendMessage(p, ChatColor.RED + "You must select a kit to use that command!");
    68. }
    69.  
    70. public static ItemStack wool() {
    71. ItemStack i = new ItemStack(Material.WOOL, 1, (short) 14);
    72. ItemMeta im = i.getItemMeta();
    73. im.setDisplayName("");
    74. i.setItemMeta(im);
    75.  
    76. return i;
    77. }
    78. }
    79.  


    And the other thing I think it is:
    Code:java
    1. package me.chasertw123.kitpvp.timers;
    2.  
    3. import me.chasertw123.kitpvp.KitPvP;
    4. import org.bukkit.Sound;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.scheduler.BukkitRunnable;
    7.  
    8. public class xpCoolDown extends BukkitRunnable {
    9.  
    10. @SuppressWarnings("unused")
    11. private KitPvP plugin;
    12.  
    13. int delay;
    14. Player p = null;
    15. int counter;
    16.  
    17.  
    18. public xpCoolDown(Player p, int delay) {
    19. this.delay = delay;
    20. this.p = p;
    21. counter = delay;
    22. }
    23.  
    24. public void run() {
    25. if(p == null){
    26.  
    27. this.cancel();
    28. }
    29. switch (this.counter) {
    30. case 10:
    31. p.setExp(5F);
    32. p.playSound(p.getLocation(), Sound.CLICK, 10, 10);
    33. break;
    34. case 9:
    35. p.setExp(5F);
    36. p.playSound(p.getLocation(), Sound.CLICK, 10, 10);
    37. break;
    38. case 8:
    39. p.setExp(5F);
    40. p.playSound(p.getLocation(), Sound.CLICK, 10, 10);
    41. break;
    42. case 7:
    43. p.setExp(5F);
    44. p.playSound(p.getLocation(), Sound.CLICK, 10, 10);
    45. break;
    46. case 6:
    47. p.setExp(5F);
    48. p.playSound(p.getLocation(), Sound.CLICK, 10, 10);
    49. break;
    50. case 5:
    51. p.setExp(5F);
    52. p.playSound(p.getLocation(), Sound.CLICK, 10, 10);
    53. break;
    54. case 4:
    55. p.setExp(4F);
    56. p.playSound(p.getLocation(), Sound.CLICK, 10, 10);
    57. break;
    58. case 3:
    59. p.setExp(3F);
    60. p.playSound(p.getLocation(), Sound.CLICK, 10, 10);
    61. break;
    62. case 2:
    63. p.setExp(2F);
    64. p.playSound(p.getLocation(), Sound.CLICK, 10, 10);
    65. break;
    66. case 1:
    67. p.setExp(1F);
    68. p.playSound(p.getLocation(), Sound.CLICK, 10, 10);
    69. break;
    70. case 0:
    71. counter = delay;
    72. this.cancel();
    73. break;
    74. }
    75.  
    76. this.counter--;
    77. }
    78.  
    79. public int getDelay() {
    80. return counter;
    81. }
    82. }


    Sorry this is such a long post I don't know how to do spoilers! :D
     
  2. Offline

    RawCode

    at me.chasertw123.kitpvp.misc.Repair.Rep(Repair.java:44) ~[?:?]

    it gives you line, just open and and trace fields.
     
  3. Offline

    chasertw123

    RawCode
    Ya I know its there. But I don't understand what is causing the error.

    This is the line I am getting the error on:
    Code:java
    1. new xpCoolDown(p, 10).runTaskTimer(plugin, 20L, 20L);


    This goes to my other class I have above. I don't understand whats causing the error though.
     
  4. Offline

    RawCode

    public static void Rep(Player p) {

    read about static keyword and what it does.
     
  5. Offline

    chasertw123

    OK.... I read about it. But I still don't understand. Sorry this is one thing I am not familiar with. If someone could maybe explain to me what I could do yo fix this that would be awesome. Please don't feed me code because I wan to actually learn.
     
  6. Offline

    RawCode

    trace all locations where you possibly use plugin field, count number of SET operations. (if you invoke method that use plugin field it still count)

    if SET operations are zero - you obviosly never set your field and it null.
     
  7. Offline

    chasertw123

    I am sorry that I am not advanced to understand what you mean; but if you could explain in a more idiot friendly way that would be awesome! :)
     
  8. Offline

    Appljuze

    RawCode It's obvious he isn't understanding it. Why would you just continue to give ambiguous answers?

    Anyway, to answer your question...

    First off, I see a lot of static variables and methods in your code. It's not smart to use static much, ESPECIALLY if you don't understand what it means. Static is not meant to be a "cheat way" to access variables from other classes. I suggest watching Episode 12 of my Bukkit for Beginners series on YouTube, which explains how objects and classes work. Once you understand how they work, it'll make it easier to understand what static means.

    In your Repair class, you have a constructor, like so:
    Code:java
    1. public Repair(KitPvP plugin) {Repair.plugin = plugin;}

    This is good! The only problem is, you're never creating a new instance of your Repair class. You're simply just trying to call the .rep() method, and that method uses a reference to your KitPvP class (called plugin), but since you haven't given plugin a value, it's null. That is why it's saying plugin cannot be null.

    Simple create a new instance of your repair class, such as Repair rep = new Repair(this). Be sure you put that in your KitPvP class, so that 'this' is referring to an instance of the KitPvP class. Then use that object (called rep) to call all of your methods. And don't make them static!

    Hope this helps.
     
  9. Offline

    RawCode

    spoonfeeding is not way of learning, i give "ambiguous" answers just to let TS solve issue self.

    current solution - remove static keyword from method and TS will be forced to create instance of class, this will solve issue.

    your long and mostly useless answer just make things ever harder to understand Appljuze
     
  10. Offline

    Appljuze

    You just contradicted yourself. Ambiguous answers just make things more difficult to understand. And you just said "spoonfeeding is not a way of learning" yet in the next sentence you simply gave him a direct answer. /rant
     
  11. Offline

    RawCode

    i stated right from the beginig that problem with static keyword....
     
Thread Status:
Not open for further replies.

Share This Page