After reading config from plugin in static - It bugs my plugin

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

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

    Tamir

    Hey guys.
    So i got the following code:
    Code:java
    1. public static Inventory GambleMenu = Bukkit.createInventory(null, 18, "Gambling Menu");
    2. static {
    3. //ItemStacks.
    4. //FirstPot
    5. ItemStack Pot1 = new ItemStack(Material.POTION, 1);
    6. ItemMeta Potion1 = Pot1.getItemMeta();
    7. Potion1.setDisplayName(plugin.getConfig().getString("FirstPotionName"));
    8. Potion1.setLore(Arrays.asList(plugin.getConfig().getString("FirstPotionPrice"), ChatColor.RED + "" + ChatColor.BOLD + "40% To win!"));
    9. Pot1.setItemMeta(Potion1);
    10.  
    11. //SecondPot
    12. ItemStack Pot2 = new ItemStack(Material.POTION, 1);
    13. ItemMeta Potion2 = Pot2.getItemMeta();
    14. Potion2.setDisplayName(plugin.getConfig().getString("SecondPotionName"));
    15. Potion1.setLore(Arrays.asList(plugin.getConfig().getString("SecondPotionPrice"), ChatColor.RED + "" + ChatColor.BOLD + "40% To win!"));
    16. Pot2.setItemMeta(Potion2);
    17.  
    18. //ThirdPot
    19. ItemStack Pot3 = new ItemStack(Material.POTION, 1);
    20. ItemMeta Potion3 = Pot3.getItemMeta();
    21. Potion3.setDisplayName(plugin.getConfig().getString("ThirdPotionName"));
    22. Potion1.setLore(Arrays.asList(plugin.getConfig().getString("ThirdPotionPrice"), ChatColor.RED + "" + ChatColor.BOLD + "40% To win!"));
    23. Pot3.setItemMeta(Potion3);
    24.  
    25. //4'thPot
    26. ItemStack Pot4 = new ItemStack(Material.POTION, 1);
    27. ItemMeta Potion4 = Pot4.getItemMeta();
    28. Potion4.setDisplayName(plugin.getConfig().getString("FourthPotionName"));
    29. Potion1.setLore(Arrays.asList(plugin.getConfig().getString("FourthPotionPrice"), ChatColor.RED + "" + ChatColor.BOLD + "40% To win!"));
    30. Pot4.setItemMeta(Potion4);
    31.  
    32. //5'thPot
    33. ItemStack Pot5 = new ItemStack(Material.POTION, 1);
    34. ItemMeta Potion5 = Pot5.getItemMeta();
    35. Potion5.setDisplayName(plugin.getConfig().getString("FifthPotionName"));
    36. Potion1.setLore(Arrays.asList(plugin.getConfig().getString("FifthPotionPrice"), ChatColor.RED + "" + ChatColor.BOLD + "40% To win!"));
    37. Pot5.setItemMeta(Potion5);
    38. //6'thPot
    39. ItemStack Pot6 = new ItemStack(Material.POTION, 1);
    40. ItemMeta Potion6 = Pot6.getItemMeta();
    41. Potion6.setDisplayName(plugin.getConfig().getString("SixPotionName"));
    42. Potion6.setLore(Arrays.asList(plugin.getConfig().getString("SixPotionPrice"), ChatColor.RED + "" + ChatColor.BOLD + "40% To win!"));
    43. Pot6.setItemMeta(Potion6);
    44. //7'thPot
    45. ItemStack Pot7 = new ItemStack(Material.POTION, 1);
    46. ItemMeta Potion7 = Pot7.getItemMeta();
    47. Potion7.setDisplayName(plugin.getConfig().getString("SevenPotionName"));
    48. Potion7.setLore(Arrays.asList(plugin.getConfig().getString("SevenPotionPrice"), ChatColor.RED + "" + ChatColor.BOLD + "40% To win!"));
    49. Pot7.setItemMeta(Potion7);
    50. //8'thPot
    51. ItemStack Pot8 = new ItemStack(Material.POTION, 1);
    52. ItemMeta Potion8 = Pot8.getItemMeta();
    53. Potion8.setDisplayName(plugin.getConfig().getString("EightPotionName"));
    54. Potion8.setLore(Arrays.asList(plugin.getConfig().getString("EightPotionPrice"), ChatColor.RED + "" + ChatColor.BOLD + "40% To win!"));
    55. Pot8.setItemMeta(Potion8);
    56. //9Th Pot
    57. ItemStack Pot9 = new ItemStack(Material.POTION, 1);
    58. ItemMeta Potion9 = Pot9.getItemMeta();
    59. Potion9.setDisplayName(plugin.getConfig().getString("NinePotionName"));
    60. Potion9.setLore(Arrays.asList(plugin.getConfig().getString("NinePotionPrice"), ChatColor.RED + "" + ChatColor.BOLD + "40% To win!"));
    61. Pot9.setItemMeta(Potion9);
    62. //PICK1
    63. ItemStack Pickaxe15 = new ItemStack(Material.DIAMOND_PICKAXE, 1);
    64. ItemMeta Pick15 = Pickaxe15.getItemMeta();
    65. Pick15.setDisplayName(plugin.getConfig().getString(ChatColor.RED + "" + ChatColor.BOLD + "15" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + " Pickaxe"));
    66. Pick15.setLore(Arrays.asList(plugin.getConfig().getString("PickaxePrice15"), ChatColor.RED + "" + ChatColor.BOLD + "10% To win!"));
    67. Pickaxe15.setItemMeta(Pick15);
    68.  
    69. //PICK2
    70. ItemStack Pickaxe20 = new ItemStack(Material.DIAMOND_PICKAXE, 1);
    71. ItemMeta Pick20 = Pickaxe20.getItemMeta();
    72. Pick20.setDisplayName(plugin.getConfig().getString(ChatColor.RED + "" + ChatColor.BOLD + "20" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + " Pickaxe"));
    73. Pick20.setLore(Arrays.asList(plugin.getConfig().getString("PickaxePrice20"), ChatColor.RED + "" + ChatColor.BOLD + "10% To win!"));
    74. Pickaxe20.setItemMeta(Pick20);
    75.  
    76. //PICK3
    77. ItemStack Pickaxe25 = new ItemStack(Material.DIAMOND_PICKAXE, 1);
    78. ItemMeta Pick25 = Pickaxe25.getItemMeta();
    79. Pick25.setDisplayName(plugin.getConfig().getString(ChatColor.RED + "" + ChatColor.BOLD + "25" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + " Pickaxe"));
    80. Pick25.setLore(Arrays.asList(plugin.getConfig().getString("PickaxePrice20"), ChatColor.RED + "" + ChatColor.BOLD + "10% To win!"));
    81. Pickaxe25.setItemMeta(Pick25);
    82.  
    83. //PICK4
    84. ItemStack Pickaxe30 = new ItemStack(Material.DIAMOND_PICKAXE, 1);
    85. ItemMeta Pick30 = Pickaxe30.getItemMeta();
    86. Pick30.setDisplayName(plugin.getConfig().getString(ChatColor.RED + "" + ChatColor.BOLD + "30" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + " Pickaxe"));
    87. Pick30.setLore(Arrays.asList(plugin.getConfig().getString("PickaxePrice20"), ChatColor.RED + "" + ChatColor.BOLD + "10% To win!"));
    88. Pickaxe30.setItemMeta(Pick30);
    89.  
    90. //PICK5
    91. ItemStack Pickaxe35 = new ItemStack(Material.DIAMOND_PICKAXE, 1);
    92. ItemMeta Pick35 = Pickaxe35.getItemMeta();
    93. Pick35.setDisplayName(plugin.getConfig().getString(ChatColor.RED + "" + ChatColor.BOLD + "35" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + " Pickaxe"));
    94. Pick35.setLore(Arrays.asList(plugin.getConfig().getString("PickaxePrice20"), ChatColor.RED + "" + ChatColor.BOLD + "10% To win!"));
    95. Pickaxe35.setItemMeta(Pick35);
    96.  
    97. //PICK6
    98. ItemStack Pickaxe40 = new ItemStack(Material.DIAMOND_PICKAXE, 1);
    99. ItemMeta Pick40 = Pickaxe40.getItemMeta();
    100. Pick40.setDisplayName(plugin.getConfig().getString(ChatColor.RED + "" + ChatColor.BOLD + "40" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + " Pickaxe"));
    101. Pick40.setLore(Arrays.asList(plugin.getConfig().getString("PickaxePrice20"), ChatColor.RED + "" + ChatColor.BOLD + "10% To win!"));
    102. Pickaxe40.setItemMeta(Pick40);
    103. //PICK7
    104. ItemStack Pickaxe45 = new ItemStack(Material.DIAMOND_PICKAXE, 1);
    105. ItemMeta Pick45 = Pickaxe45.getItemMeta();
    106. Pick45.setDisplayName(plugin.getConfig().getString(ChatColor.RED + "" + ChatColor.BOLD + "45" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + " Pickaxe"));
    107. Pick45.setLore(Arrays.asList(plugin.getConfig().getString("PickaxePrice20"), ChatColor.RED + "" + ChatColor.BOLD + "10% To win!"));
    108. Pickaxe45.setItemMeta(Pick45);
    109. //PICK8
    110. ItemStack Pickaxe50 = new ItemStack(Material.DIAMOND_PICKAXE, 1);
    111. ItemMeta Pick50 = Pickaxe50.getItemMeta();
    112. Pick50.setDisplayName(plugin.getConfig().getString(ChatColor.RED + "" + ChatColor.BOLD + "50" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + " Pickaxe"));
    113. Pick50.setLore(Arrays.asList(plugin.getConfig().getString("PickaxePrice20"), ChatColor.RED + "" + ChatColor.BOLD + "10% To win!"));
    114. Pickaxe50.setItemMeta(Pick50);
    115. //PICK9
    116. ItemStack Pickaxe60 = new ItemStack(Material.DIAMOND_PICKAXE, 1);
    117. ItemMeta Pick60 = Pickaxe60.getItemMeta();
    118. Pick60.setDisplayName(plugin.getConfig().getString(ChatColor.RED + "" + ChatColor.BOLD + "50" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + " Pickaxe"));
    119. Pick60.setLore(Arrays.asList(plugin.getConfig().getString("PickaxePrice20"), ChatColor.RED + "" + ChatColor.BOLD + "10% To win!"));
    120. Pickaxe60.setItemMeta(Pick60);
    121. //EndOfItemstacks
    122. GambleMenu.setItem(0, new ItemStack(Pot1));
    123. GambleMenu.setItem(1, new ItemStack(Pot2));
    124. GambleMenu.setItem(2, new ItemStack(Pot3));
    125. GambleMenu.setItem(3, new ItemStack(Pot4));
    126. GambleMenu.setItem(4, new ItemStack(Pot5));
    127. GambleMenu.setItem(5, new ItemStack(Pot6));
    128. GambleMenu.setItem(6, new ItemStack(Pot7));
    129. GambleMenu.setItem(7, new ItemStack(Pot8));
    130. GambleMenu.setItem(8, new ItemStack(Pot9));
    131. GambleMenu.setItem(9, new ItemStack(Pickaxe15));
    132. GambleMenu.setItem(10, new ItemStack(Pickaxe20));
    133. GambleMenu.setItem(11, new ItemStack(Pickaxe25));
    134. GambleMenu.setItem(12, new ItemStack(Pickaxe30));
    135. GambleMenu.setItem(13, new ItemStack(Pickaxe35));
    136. GambleMenu.setItem(14, new ItemStack(Pickaxe40));
    137. GambleMenu.setItem(15, new ItemStack(Pickaxe45));
    138. GambleMenu.setItem(16, new ItemStack(Pickaxe50));
    139. GambleMenu.setItem(17, new ItemStack(Pickaxe60));
    140. }
    141.  

    Thats how i set plugin:
    Code:java
    1. private static Main plugin;
    2. @SuppressWarnings("static-access")
    3. public void CensoredWordProcess(Main plugin) {
    4. this.plugin = plugin;
    5. }

    So, How can i fix it?
    I tested and Im pretty sure that the config causes it.
    Thanks in advance,
    Tamir :)
     
  2. Offline

    Niknea

    Tamir Only make the variable "main" static in your MAIN class, in every other class don't include the static, you can then remove the SuppressWarning.
     
  3. Offline

    Tamir

    Niknea
    Well, I changed it to this in my MAIN class:
    Code:java
    1. private static Main plugin;
    2. public void CensoredWordProcess(Main plugin) {
    3. Main.plugin = plugin;
    4. }
    5.  

    Still bugged.
     
  4. Offline

    Niknea

    Tamir In your main class you don't need to create a constructor like you did. Only in the other classes your trying to get an instance of the main class.
     
  5. Offline

    Tamir

    Niknea
    Well, If i don't, It can't really read "plugin".
    EDIT: Nvm, I understood you worng.
    SECOND EDIT: Still not working, I tried whatever you told me to.
     
  6. Offline

    Niknea

    Tamir Can I see your new code?
     
  7. Offline

    Tamir

    Niknea
    I created another class to storage it,
    Code:java
    1. private static InventoryManager plugin = new InventoryManager();
    2.  

    It dosen't work, It keeps bugging stuff.
    Actully, Intilazer Error.
     
  8. Offline

    Niknea

    Tamir

    Have this in your main class :

    Code:java
    1. public static ClassName plugin;


    In all the classes you want to get an instance of the main class, add the following:

    Code:java
    1. MainClassName plugin;
    2.  
    3. public ClassName(Main plugin){
    4. this.plugin = plugin;
    5. }
     
  9. Offline

    stormneo7

    Potions 1 - 5 have
    Potion1.setLore() instead of 1, 2, 3, 4, 5.
    That's not the problem but just pointing that out.
     
  10. Offline

    Traks

    The problem is that the static initializer invokes plugin.getConfig() before the 'plugin' variable has been initialised, thus 'plugin' is null and a NullPointerException is thrown. There are several ways to fix this, although I recommend getting rid of statics completely, since they violate the main concept of object orientated programming, in my opinion. By the way, utilising an instance initializer block wouldn't make it work either.
     
    TigerHix and teozfrank like this.
  11. Offline

    AoH_Ruthless

    Tamir
    Kind of offtopic but you could make your class from main post (with static constructor) a lot cleaner by:
    • Using loops and mapping (instead of pot1,pot2, pot3, etc...)
    • Have separate methods for many of these and call these methods in the constructor.
    However, as Traks pointed out, you haven't yet initialized your plugin variable ... so it is null -> NPE.
     
  12. Offline

    Tamir

    Niknea
    It gives me an error when i use the command
    "An internal error eccourd..."
    Code:java
    1. public static InventoryManager plugin;
    2.  
    3. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    4. Player player = (Player) sender;
    5. if(commandLabel.equalsIgnoreCase("Gamble")){
    6. // &8&lTh&4&li&8&lef
    7. player.openInventory(plugin.GambleMenu);
    8.  
    9. LivingEntity entity1 = (LivingEntity) player.getWorld().spawnEntity(player.getLocation(), EntityType.VILLAGER);
    10. entity1.setCustomName(ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "Th" + ChatColor.DARK_RED + "" + ChatColor.BOLD + "i" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "ef");
    11.  
    12. }
    13. return false;
    14. }
    15.  

    Unhandled exception error.
     
  13. Offline

    Niknea

    Tamir StackTrace please.
     
  14. your ClassName declaration in the main class needs to be initialised or assigned to some value, other wise it will throw null. :p

    in OnEnable add:

    Code:java
    1. plugin = this;


    Thats one of two approaches you can take. I generally avoid static completly where possible. There are occasions where you will actually need to use them.
     
  15. Offline

    Tamir

    teozfrank
    Its still throwing NPE :l
    Edit: It seems like the line
    Code:java
    1. player.openInventory(plugin.GambleMenu);
    2.  

    Is bugging it
     
  16. Offline

    Dragonphase

    Tamir

    Your GambleMenu Inventory is null or uninstantiated. Or your plugin variable.
     
  17. Offline

    Tamir

    Dragonphase
    What can i use instead of null then?
    Code:java
    1. public static Inventory GambleMenu = Bukkit.createInventory(null, 18, "Gambling Menu"));
    2.  
     
  18. Offline

    Traks

    The 'plugin' variable is null, I believe I told you this earlier on in this thread. Just create an instance variable and set its value through a constructor, and pass the active instance of your main class through it. Or if the onCommand method (and 'plugin' variable) is in your main class, use plugin = this.
     
  19. Offline

    Dragonphase

    Tamir

    You have two closing parentheses at the end of that line.
     
  20. Offline

    Tamir

    Traks
    Tried, Didnt help.
    Dragonphase
    Its a little mistake that i didnt notice, After i had a problem i fixed it in a second.
    But i cant set it to anything else except null
    EDIT: If i switch to one class back, How do i define plugin there?
     
  21. Offline

    Dragonphase

    Tamir

    Pass the plugin by value and make sure it isn't static:

    Code:java
    1. private Main plugin;
    2.  
    3. public void CensoredWordProcess(Main plugin){
    4. this.plugin = plugin;
    5. }


    It's best to avoid statics where they aren't necessary.
     
    Niknea likes this.
  22. If there is a missing parentheses or not enough the plugin wont even complile you know :p
     
  23. Offline

    Dragonphase

    teozfrank

    I know, but it's still better to point it out and make them aware of any mistakes they've made.
     
Thread Status:
Not open for further replies.

Share This Page