[UNSOLVED]Small Console Error Which I Can't Seem To Fix

Discussion in 'Plugin Development' started by callum2904, May 18, 2013.

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

    callum2904

    Hi Keeping This Short!

    My Main Class:
    Code:java
    1. package me.callum2904.Horde;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import me.callum2904.Horde.LargeScaleEvents.PlayerSignShop;
    6. import me.callum2904.Horde.MediumScaleEvents.EntityCounter;
    7.  
    8. import org.bukkit.plugin.PluginManager;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class Main extends JavaPlugin{
    12. public ArrayList<String> toggleShop;
    13.  
    14. public void onEnable(){
    15. this.toggleShop = new ArrayList<String>();
    16. getConfig().options().copyDefaults(true);
    17. saveConfig();
    18.  
    19. PluginManager pm = this.getServer().getPluginManager();
    20. pm.registerEvents(new EntityCounter(this), this);
    21. pm.registerEvents(new PlayerSignShop(this), this);
    22.  
    23. }
    24.  
    25. public void onDisable(){
    26.  
    27. }
    28. }
    29. [syntax=java]
    30.  
    31. My Class Which i think is causing the problems:
    32. [syntax=java]package me.callum2904.Horde.LargeScaleEvents;
    33.  
    34. import java.util.ArrayList;
    35. import java.util.HashMap;
    36.  
    37. import me.callum2904.Horde.Main;
    38.  
    39. import org.bukkit.ChatColor;
    40. import org.bukkit.Location;
    41. import org.bukkit.entity.Player;
    42. import org.bukkit.event.EventHandler;
    43. import org.bukkit.event.Listener;
    44. import org.bukkit.event.block.Action;
    45. import org.bukkit.event.block.BlockBreakEvent;
    46. import org.bukkit.event.block.SignChangeEvent;
    47. import org.bukkit.event.player.PlayerInteractEvent;
    48.  
    49. public class PlayerSignShop implements Listener{
    50.  
    51. public ArrayList<String> toggleShop;
    52. public Main plugin;
    53.  
    54. public PlayerSignShop(Main plugin){
    55. this.plugin = plugin;
    56. }
    57.  
    58. public final HashMap<Location, String> signs = new HashMap<Location, String>();
    59. int NextNumber = plugin.getConfig().getInt("SignShops." + "NextNumber");
    60. @EventHandler
    61. public void onSignChange(SignChangeEvent event){
    62. if(event.getLine(0).contains("hordeshop") && event.getLine(1).equals("default")){
    63. signs.put(event.getBlock().getLocation(), event.getPlayer().getName());
    64. event.setLine(0, ChatColor.GOLD + "[HordeShop]");
    65. event.setLine(1, ChatColor.DARK_RED + "Default");
    66. event.setLine(3, ChatColor.BLACK + "Right_Click");
    67. NextNumber ++;
    68. plugin.getConfig().set("SignShops." + "NextNumber", NextNumber);
    69. plugin.getConfig().set("SignShops." + NextNumber + ".Type", event.getLine(1));
    70. plugin.getConfig().set("SignShops." + NextNumber + ".Location" + ".X", event.getBlock().getX());
    71. plugin.getConfig().set("SignShops." + NextNumber + ".Location" + ".Y", event.getBlock().getY());
    72. plugin.getConfig().set("SignShops." + NextNumber + ".Location" + ".Z", event.getBlock().getZ());
    73. plugin.saveConfig();
    74. }
    75. }
    76.  
    77. @EventHandler
    78. public void BlockBreak(BlockBreakEvent event){
    79. if(signs.containsKey(event.getBlock()) && !signs.containsValue(event.getPlayer().getName())){
    80. signs.remove(event.getBlock());
    81. signs.remove(event.getPlayer().getName());
    82. if(plugin.getConfig().getInt("SignShops." + NextNumber + ".Location" + ".X") == event.getBlock().getX()
    83. && plugin.getConfig().getInt("SignShops." + NextNumber + ".Location" + ".Y") == event.getBlock().getY()
    84. && plugin.getConfig().getInt("SignShops." + NextNumber + ".Location" + ".Z") == event.getBlock().getZ()){
    85. plugin.getConfig().set("SignShops." + NextNumber + ".Location" + ".X", 0);
    86. plugin.getConfig().set("SignShops." + NextNumber + ".Location" + ".Y", 0);
    87. plugin.getConfig().set("SignShops." + NextNumber + ".Location" + ".Z", 0);
    88. plugin.saveConfig();
    89. }
    90. }
    91. }
    92.  
    93. @EventHandler
    94. public void Shop(PlayerInteractEvent event){
    95. Player player = event.getPlayer();
    96. if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK)
    97. && plugin.getConfig().getInt("SignShops." + NextNumber + ".Location" + ".X") == event.getClickedBlock().getX()
    98. && plugin.getConfig().getInt("SignShops." + NextNumber + ".Location" + ".Y") == event.getClickedBlock().getY()
    99. && plugin.getConfig().getInt("SignShops." + NextNumber + ".Location" + ".Z") == event.getClickedBlock().getZ()){
    100. player.sendMessage("Shop");
    101. }
    102. }
    103.  
    104. }
    105. [syntax=java]
    106.  
    107. Heres The Errors
    108. [syntax=java]18:04:48 [SEVERE] Error occurred while enabling Horde v0.1.0 (Is it up to date?)
    109.  
    110. at me.callum2904.Horde.LargeScaleEvents.PlayerSignShop.<init>(PlayerSign
    111. Shop.java:28)
    112. at me.callum2904.Horde.Main.onEnable(Main.java:21)
    113. at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
    114. at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    115. .java:457)
    116. at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    117. r.java:381)
    118. at org.bukkit.craftbukkit.v1_5_R3.CraftServer.loadPlugin(CraftServer.jav
    119. a:282)
    120. at org.bukkit.craftbukkit.v1_5_R3.CraftServer.enablePlugins(CraftServer.
    121. java:264)
    122. at org.bukkit.craftbukkit.v1_5_R3.CraftServer.reload(CraftServer.java:60
    123. 5)
    124. at org.bukkit.Bukkit.reload(Bukkit.java:185)
    125. at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
    126. 23)
    127. at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    128. 9)
    129. at org.bukkit.craftbukkit.v1_5_R3.CraftServer.dispatchCommand(CraftServe
    130. r.java:523)
    131. at org.bukkit.craftbukkit.v1_5_R3.CraftServer.dispatchServerCommand(Craf
    132. tServer.java:512)
    133. at net.minecraft.server.v1_5_R3.DedicatedServer.an(DedicatedServer.java:
    134. 262)
    135. at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:2
    136. 27)
    137. at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:4
    138. 77)
    139. at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java
    140. :410)
    141. at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:5
    142. 73)[syntax=java]
    143.  
    144. Thanks In Advance![/syntax][/syntax][/syntax][/syntax][/syntax]
     
  2. Offline

    Ultimate_n00b

    Whats the error?
     
  3. Offline

    callum2904

    Ultimate_n00b I added The Errors I though something was missing
     
  4. Offline

    Ultimate_n00b

    Replace all your [ code ] with [.syntax=java] so I can see the lines.
    And on the sign one, check line 28.
     
  5. Offline

    callum2904

    Ultimate_n00b done but its made it all into 1 giant piece of code
     
  6. Offline

    Ultimate_n00b

    Replace the [ /code ] with [ /syntax ] (without spaces of course.)

    Figured it out.
    The error is on line 28 of the second class:
    int NextNumber = plugin.getConfig().getInt("SignShops." + "NextNumber");
    Just guessing, The path "SignShops." + ... wait a sec... is NextNumber a variable? Why does it have quotes?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  7. Offline

    callum2904

    so do you know what i need to change?
     
  8. Offline

    Ultimate_n00b

    int NextNumber = plugin.getConfig().getInt("SignShops." + "NextNumber");
    NextNumber is a variable right?
    If so, why is it in quotes?
     
  9. Offline

    callum2904

    oh sorry about the last message making you post again. NextNumber is both an int and a path from a the config.
     
  10. Offline

    Ultimate_n00b

    Yeah, you had it in quotes. So its going to SignShops -> NextNumber, which is null, since it isn't set.
     
  11. Offline

    callum2904

    what does that mean? in the config at my workspace i have the path like this:

    SignShops:
    NextNumber: 0
     
  12. Offline

    Ultimate_n00b

    Then why do you have it "SignShops" + "NextNumber"? Just do "SignShops.NextNumber".. But still, thats whats returning null
     
  13. Offline

    callum2904

    ok so how would i fix that to return whats in the config?

    How do i fix that?

    ANYONE??? BUMP

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  14. Offline

    lenis0012

    Integers cant return null smartass

    Your problem is that the global fields get created before the instance is created.
    So it should be like this:
    Code:java
    1. private Main plugin;
    2. private int nextNumber;
    3.  
    4. public PlayerSignShop(Main plugin) {
    5. this.plugin = plugin;
    6. this.nextNumber = plugin.getConfig().getInt("SignsShops.NextNumber", 0); //Done after the plugin field is set so that it cant be null
    7. }


    It is just a tp to strart all fields without a value and add defaults to it when the class gets inited.
    This prevents things being called to early

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  15. Offline

    slayr288

    lenis0012

    You're trying to get a int that doesn't exist in the config, hence the NPE.
     
  16. slayr288
    If you're asigning a null-returning method to a variable it's just fine, a NPE is thrown when you use a method from that pointer (whch is null), therefore 'null.method()' can not happen, but 'var = null' can happen.

    Also 'int' can't be null, and getInt() returns int, in the case that it doesn't exist it will return 0... which, by the way, also has a default value of 0 in case the setting does not exist, so the config value is not the problem at all.
     
  17. Offline

    lenis0012

    Digi slayr288

    Thats what im trying to make clear.
    An int can never return 'null'
    Wich means that the NullPointerException is thrown because the plugin field is null.
    And it is null because the fields are inited before the class instance is called.
    Wich will result in a 'null' for the field 'plugin'.
    To fix this you would have to move some stuff over to the constructor
     
Thread Status:
Not open for further replies.

Share This Page