Class problems

Discussion in 'Plugin Development' started by bobthefish, Jul 23, 2014.

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

    bobthefish

    hi

    I wasnt sure exactly what to call this, thats why the name is so vague :p but what my problem is is that I get an error whenever I try to get the main class from another class.

    here is my error:

    Code:
    Error occurred while enabling KitPvp version beta-1.6 (Is it up to date?)
    java.lang.ExceptionInInitializerError
        at me.BobTheFish.Kitpvp.mainKitPvp.onEnable(mainKitPvp.java:42) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:316) ~[server.jar:git-Spigot-1551]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:329) [server.jar:git-Spigot-1551]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [server.jar:git-Spigot-1551]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugin(CraftServer.java:476) [server.jar:git-Spigot-1551]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.enablePlugins(CraftServer.java:394) [server.jar:git-Spigot-1551]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.reload(CraftServer.java:866) [server.jar:git-Spigot-1551]
        at org.bukkit.Bukkit.reload(Bukkit.java:301) [server.jar:git-Spigot-1551]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23) [server.jar:git-Spigot-1551]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) [server.jar:git-Spigot-1551]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) [server.jar:git-Spigot-1551]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchServerCommand(CraftServer.java:753) [server.jar:git-Spigot-1551]
        at net.minecraft.server.v1_7_R4.DedicatedServer.aB(DedicatedServer.java:326) [server.jar:git-Spigot-1551]
        at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:290) [server.jar:git-Spigot-1551]
        at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [server.jar:git-Spigot-1551]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [server.jar:git-Spigot-1551]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [server.jar:git-Spigot-1551]
    Caused by: java.lang.NullPointerException
        at me.BobTheFish.Kitpvp.KitInventory.makerOP(KitInventory.java:97) ~[?:?]
        at me.BobTheFish.Kitpvp.KitInventory.<clinit>(KitInventory.java:33) ~[?:?]
        ... 17 more
    and then here is my main's on enable:

    Code:java
    1. public void onEnable(){
    2. this.logger.info("[KitPvp] Has Been Enabled!");
    3.  
    4. new BlockBreakListener(this);
    5. new JoinListener(this);
    6. new OpenInventoryListener(this);
    7. new InventoryClickListener(this);
    8. new DamageListener(this);
    9. new FoodLevelListener(this);
    10. new RespawnListener(this);
    11. new DeathListener(this);
    12. new StatsDeathListener(this);
    13. new ArenaInventory(this);
    14. new ListenerLeave(this);
    15. new ListenerPickUp(this);
    16. new ListenerDrop(this);
    17. new KitInventory(this);
    18. new OPKitCactus(this);
    19. getCommand("stats").setExecutor(new StatsCommand(this));
    20. getCommand("na").setExecutor(new NewArenaCommand(this));
    21. getCommand("s").setExecutor(new SpawnCommand(this));
    22. getCommand("ss").setExecutor(new SetSpawnCommand(this));
    23. ai = new File(getDataFolder(), "AI.yml");
    24. aic = YamlConfiguration.loadConfiguration(ai);
    25.  
    26. if(!ai.exists()){
    27. try {
    28. ai.createNewFile();
    29. } catch (IOException e) {
    30. e.printStackTrace();
    31. }
    32. }
    33. ArenaInventory.myInventory.setContents(loadInv("myInventory"));
    34. playerYml = new File(getDataFolder(), "players.yml");
    35. playerConfig = YamlConfiguration.loadConfiguration(playerYml);
    36.  
    37. if(!playerYml.exists()) {
    38. try {
    39. playerYml.createNewFile();
    40. }
    41. catch (IOException e) {
    42. Bukkit.getServer().getLogger().severe("Could not make the players.yml in the SiplePlayDisplay folder!");
    43. }
    44. }
    45. }


    and my KitInventory class:

    Code:java
    1. static mainKitPvp plugin;
    2.  
    3. public KitInventory(mainKitPvp instance) {
    4. KitInventory.plugin = instance;
    5. }
    6. public static Inventory kit = Bukkit.createInventory(null, 27, "Pick a kit!");
    7. static {
    8.  
    9. ItemStack m = new ItemStack(maker(Material.BOW,"Sniper",ChatColor.GRAY));
    10. ItemStack q = new ItemStack(maker(Material.IRON_HELMET,"Pro",ChatColor.GRAY));
    11. ItemStack w = new ItemStack(maker2("Wizard", ChatColor.AQUA));
    12. ItemStack e = new ItemStack(maker3(Material.SKULL_ITEM, 1, 0, "Assassin",ChatColor.GRAY));
    13. ItemStack r = new ItemStack(maker(Material.GOLD_HELMET,"Dragon",ChatColor.RED));
    14. ItemStack t = new ItemStack(maker4("Saboteur",ChatColor.DARK_GREEN));
    15. ItemStack y = new ItemStack(maker(Material.GOLD_CHESTPLATE,"Paladin",ChatColor.GOLD));
    16. ItemStack u = new ItemStack(maker(Material.DIAMOND_CHESTPLATE,"Juggernaut",ChatColor.AQUA));
    17. ItemStack i = new ItemStack(maker(Material.STICK,"Martial Arts Master",ChatColor.WHITE));
    18. ItemStack op1 = new ItemStack(makerOP(Material.CACTUS,150,"Cactus",ChatColor.DARK_GREEN));
    19.  
    20.  
    21.  
    22. kit.setItem(0,m);
    23. kit.setItem(1,q);
    24. kit.setItem(2, w);
    25. kit.setItem(3, e);
    26. kit.setItem(4, r);
    27. kit.setItem(5, t);
    28. kit.setItem(6, y);
    29. kit.setItem(7, u);
    30. kit.setItem(8, i);
    31. kit.setItem(18, op1);
    32. }
    33.  
    34.  
    35. public static ItemStack maker(Material mat, String name, ChatColor color){
    36. ArrayList<String> ls = new ArrayList<String>();
    37. ls.add(ChatColor.BLUE + "Price: " + ChatColor.WHITE + "Free");
    38. ItemStack it = new ItemStack(mat, 1);
    39. ItemMeta m = it.getItemMeta();
    40. m.setLore(ls);
    41. m.setDisplayName(color + name);
    42. it.setItemMeta(m);
    43. return it;
    44. }
    45. public static ItemStack maker2(String name, ChatColor color){
    46. ArrayList<String> ls = new ArrayList<String>();
    47. ls.add(ChatColor.BLUE + "Price: " + ChatColor.WHITE + "Free");
    48. Potion p = new Potion(PotionType.INSTANT_HEAL, 2);
    49. ItemStack it = p.toItemStack(1);
    50. ItemMeta m = it.getItemMeta();
    51. m.setLore(ls);
    52. m.setDisplayName(color + name);
    53. it.setItemMeta(m);
    54. return it;
    55. }
    56. public static ItemStack maker3(Material mat, int i,int a, String name, ChatColor color){
    57. ArrayList<String> ls = new ArrayList<String>();
    58. ls.add(ChatColor.BLUE + "Price: " + ChatColor.WHITE + "Free");
    59. ItemStack it = new ItemStack(mat, i, (short) a);
    60. ItemMeta m = it.getItemMeta();
    61. m.setLore(ls);
    62. m.setDisplayName(color + name);
    63. it.setItemMeta(m);
    64. return it;
    65. }
    66. public static ItemStack maker4(String name, ChatColor color){
    67. ArrayList<String> ls = new ArrayList<String>();
    68. ls.add(ChatColor.BLUE + "Price: " + ChatColor.WHITE + "Free");
    69. Potion p = new Potion(PotionType.POISON, 1);
    70. p.setSplash(true);
    71. ItemStack it = p.toItemStack(1);
    72. ItemMeta m = it.getItemMeta();
    73. m.setLore(ls);
    74. m.setDisplayName(color + name);
    75. it.setItemMeta(m);
    76. return it;
    77. }
    78. public static ItemStack makerOP(Material mat, int price, String name, ChatColor color){
    79. plugin.getConfig().set("Kits." + name + ".price", price);s
    80. ArrayList<String> ls = new ArrayList<String>();
    81. ls.add(ChatColor.BLUE + "Price: " + ChatColor.WHITE + price);
    82. ItemStack it = new ItemStack(mat);
    83. plugin.getConfig().set("Kits." + name + ".price", price);
    84. ItemMeta m = it.getItemMeta();
    85. m.setLore(ls);
    86. m.setDisplayName(color + name);
    87. it.setItemMeta(m);
    88. return it;
    89. }
    90. }


    any help figuring out the problem would be great :p
     
  2. Offline

    Traks

    The problem is that your static initialiser indirectly invokes plugin.getConfig() (through #makerOP), while the 'plugin' variable is still null, thus resulting in a NullPointerException. Class initialisation is done preparatory to instance initialisation. There are multiple ways to fix this, but I recommend replacing all of the statics (initialisers, methods and variables) with non-statics. You should only use statics when inevitable, since they violate the main concept of object orientated programming, in my opinion.
     
Thread Status:
Not open for further replies.

Share This Page