StackOverflowError, anyone know how to fix?

Discussion in 'Plugin Development' started by Kassestral, May 21, 2014.

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

    Kassestral

    Now I am getting this error:
    Code:
    [00:37:06 ERROR]: Could not pass event BlockBreakEvent to Levels v0.1
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:294) ~[bukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[bukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [bukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [bukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.PlayerInteractManager.breakBlock(PlayerInteractManager.java:263) [bukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.PlayerInteractManager.a(PlayerInteractManager.java:191) [bukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:549) [bukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInBlockDig.a(SourceFile:53) [bukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInBlockDig.handle(SourceFile:8) [bukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [bukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [bukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [bukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [bukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [bukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [bukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [bukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    Caused by: java.lang.NullPointerException
        at Skills.Mining.onBlockBreak(Mining.java:25) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_05]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_05]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_05]
        at java.lang.reflect.Method.invoke(Method.java:483) ~[?:1.8.0_05]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:292) ~[bukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        ... 15 more
    Levels.class(Main Class)
    Code:java
    1. package Kassestral.Levels;
    2.  
    3. import java.io.File;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.plugin.PluginManager;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. import Skills.Mining;
    10. import Skills.Smithing;
    11.  
    12. public class Levels extends JavaPlugin
    13. {
    14. PluginManager pm = Bukkit.getServer().getPluginManager();;
    15. public void onEnable()
    16. {
    17. CreateDirectories();
    18. pm.registerEvents(new API(), this);
    19. pm.registerEvents(new Handlers(), this);
    20. pm.registerEvents(new Smithing(), this);
    21. pm.registerEvents(new Mining(), this);
    22. }
    23.  
    24. public void onDisable()
    25. {
    26.  
    27. }
    28.  
    29. private void CreateDirectories()
    30. {
    31. // Creates Main Directory
    32. File MainDir = new File(getDataFolder() + "/");
    33. if(!MainDir.exists())
    34. {
    35. MainDir.mkdir();
    36. }
    37. // Creates Main Directory
    38.  
    39. // Creates Players Directory
    40. File PlayersDir = new File(getDataFolder() + "/" + File.separator + "players");
    41. if(!PlayersDir.exists())
    42. {
    43. PlayersDir.mkdir();
    44. getLogger().info("Folders Created");
    45. }
    46. // Creates Players Directory
    47. }
    48.  
    49.  
    50. }


    Handlers.class
    Code:java
    1. package Kassestral.Levels;
    2.  
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.player.PlayerJoinEvent;
    7.  
    8. public class Handlers implements Listener
    9. {
    10. API api;
    11. public API getAPI(){
    12. return api;
    13. }
    14.  
    15. //Handlers Begin
    16. @EventHandler
    17. public void onPlayerJoin(PlayerJoinEvent e)
    18. {
    19. Player p = e.getPlayer();
    20. api.CreatePlayerFile(p);
    21. }
    22. }


    API.class
    Code:java
    1. package Kassestral.Levels;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5.  
    6. import org.bukkit.configuration.file.FileConfiguration;
    7. import org.bukkit.configuration.file.YamlConfiguration;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.Listener;
    10.  
    11. public class API implements Listener
    12. {
    13. FileConfiguration PlayerConfig;
    14. File PlayerFile;
    15. Player player;
    16.  
    17.  
    18.  
    19. Levels levels;
    20. public Levels getlevel(){
    21. return getLevels();
    22. }
    23.  
    24. //Methods Start
    25. public void LPlayer(Player p)
    26. {
    27. this.player = p;
    28. this.PlayerFile = new File(getLevels().getDataFolder() + "/" + File.separator + "players" + File.separator + p.getUniqueId().toString() + ".yml");
    29. this.PlayerConfig = YamlConfiguration.loadConfiguration(this.PlayerFile);
    30. }
    31.  
    32. public Player getPlayer() {
    33. return this.player;
    34. }
    35.  
    36. public String getName() {
    37. return this.player.getName();
    38. }
    39.  
    40. public File getUserFile() {
    41. return this.PlayerFile;
    42. }
    43.  
    44. public FileConfiguration getUserConfig() {
    45. return this.PlayerConfig;
    46. }
    47.  
    48. public void save()
    49. {
    50. try {
    51. this.PlayerConfig.save(this.PlayerFile);
    52. } catch (IOException e) {
    53. e.printStackTrace();
    54. }
    55. }
    56.  
    57. public void CreatePlayerFile(Player p)
    58. {
    59. this.PlayerFile = new File(getLevels().getDataFolder() + "/" + File.separator + "players" + File.separator + p.getUniqueId().toString() + ".yml");
    60. this.PlayerConfig = YamlConfiguration.loadConfiguration(this.PlayerFile);
    61.  
    62. if(!PlayerFile.exists()){
    63. try{
    64. PlayerFile.createNewFile();
    65. PlayerConfig = YamlConfiguration.loadConfiguration(PlayerFile);
    66. PlayerConfig.createSection("Levels.Farming");
    67. PlayerConfig.set("Levels.Farming", 0);
    68. PlayerConfig.createSection("Levels.Mining");
    69. PlayerConfig.set("Levels.Mining", 0);
    70. PlayerConfig.createSection("Levels.Smithing");
    71. PlayerConfig.set("Levels.Smithing", 0);
    72. PlayerConfig.createSection("Levels.Hunting");
    73. PlayerConfig.set("Levels.Hunting", 0);
    74. PlayerConfig.createSection("Levels.Cooking");
    75. PlayerConfig.set("Levels.Cooking", 0);
    76. PlayerConfig.createSection("Levels.Combat");
    77. PlayerConfig.set("Levels.Combat", 0);
    78. PlayerConfig.createSection("Levels.Archery");
    79. PlayerConfig.set("Levels.Archery", 0);
    80. PlayerConfig.save(PlayerFile);
    81. }
    82. catch (IOException e){
    83. e.printStackTrace();
    84. }
    85. }
    86. }
    87.  
    88. public int getXP(String level)
    89. {
    90. return this.PlayerConfig.getInt(level);
    91. }
    92.  
    93. public void setXP(String s, int i)
    94. {
    95. getUserConfig().set(s, Integer.valueOf(i));
    96. save();
    97. }
    98.  
    99. public void addExp(String s, int i)
    100. {
    101. int before = getXP(s);
    102. before += i;
    103. setXP(s, before);
    104. }
    105.  
    106. //Methods End
    107.  
    108. public Kassestral.Levels.Levels getLevels() {
    109. return levels;
    110. }
    111. }


    Mining.class
    Code:java
    1. package Skills;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.block.BlockBreakEvent;
    7.  
    8. import Kassestral.Levels.API;
    9.  
    10. public class Mining implements Listener
    11. {
    12. API api;
    13. public API getAPI(){
    14. return api;
    15. }
    16.  
    17. @EventHandler
    18. public void onBlockBreak(BlockBreakEvent e)
    19. {
    20. Material Block = e.getBlock().getType();
    21. String s = "Levels.Mining";
    22.  
    23. if(Block == Material.STONE)
    24. {
    25. api.addExp(s, 5);
    26. }
    27. if(Block == Material.COAL_ORE)
    28. {
    29. api.addExp(s, 20);
    30. }
    31. if(Block == Material.LAPIS_ORE)
    32. {
    33. api.addExp(s, 20);
    34. }
    35. if(Block == Material.IRON_ORE)
    36. {
    37. api.addExp(s, 25);
    38. }
    39. if(Block == Material.GOLD_ORE)
    40. {
    41. api.addExp(s, 50);
    42. }
    43. if(Block == Material.DIAMOND_ORE)
    44. {
    45. api.addExp(s, 100);
    46. }
    47. if(Block == Material.EMERALD_ORE)
    48. {
    49. api.addExp(s, 250);
    50. }
    51. }
    52. }


    Smithing.class
    Code:java
    1. package Skills;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.inventory.InventoryClickEvent;
    7. import org.bukkit.event.inventory.InventoryType.SlotType;
    8. import org.bukkit.inventory.ItemStack;
    9.  
    10. import Kassestral.Levels.API;
    11.  
    12. public class Smithing implements Listener
    13. {
    14. API api;
    15. public API getAPI(){
    16. return api;
    17. }
    18.  
    19. @EventHandler
    20. public void onItemCraft(InventoryClickEvent e)
    21. {
    22. if(e.getSlotType() == SlotType.RESULT)
    23. {
    24. String S = "Levels.Smithing";
    25.  
    26. ItemStack l_helm = new ItemStack(Material.LEATHER_HELMET);
    27. ItemStack l_chest = new ItemStack(Material.LEATHER_CHESTPLATE);
    28. ItemStack l_legs = new ItemStack(Material.LEATHER_LEGGINGS);
    29. ItemStack l_boots = new ItemStack(Material.LEATHER_BOOTS);
    30.  
    31. ItemStack i_helm = new ItemStack(Material.IRON_HELMET);
    32. ItemStack i_chest = new ItemStack(Material.IRON_CHESTPLATE);
    33. ItemStack i_legs = new ItemStack(Material.IRON_LEGGINGS);
    34. ItemStack i_boots = new ItemStack(Material.IRON_BOOTS);
    35. ItemStack i_sword = new ItemStack(Material.IRON_SWORD);
    36.  
    37. ItemStack d_helm = new ItemStack(Material.DIAMOND_HELMET);
    38. ItemStack d_chest = new ItemStack(Material.DIAMOND_CHESTPLATE);
    39. ItemStack d_legs = new ItemStack(Material.DIAMOND_LEGGINGS);
    40. ItemStack d_boots = new ItemStack(Material.DIAMOND_BOOTS);
    41. ItemStack d_sword = new ItemStack(Material.DIAMOND_SWORD);
    42.  
    43.  
    44. //Leather Recipes
    45. if(e.getResult().equals(l_helm))
    46. {
    47. api.addExp(S, 50);
    48. }
    49. if(e.getResult().equals(l_chest))
    50. {
    51. api.addExp(S, 80);
    52. }
    53. if(e.getResult().equals(l_legs))
    54. {
    55. api.addExp(S, 70);
    56. }
    57. if(e.getResult().equals(l_boots))
    58. {
    59. api.addExp(S, 40);
    60. }
    61. //Leather Recipes End
    62.  
    63. //Iron Recipes
    64. if(e.getResult().equals(i_helm))
    65. {
    66. api.addExp(S, 125);
    67. }
    68. if(e.getResult().equals(i_chest))
    69. {
    70. api.addExp(S, 200);
    71. }
    72. if(e.getResult().equals(i_legs))
    73. {
    74. api.addExp(S, 175);
    75. }
    76. if(e.getResult().equals(i_boots))
    77. {
    78. api.addExp(S, 100);
    79. }
    80. if(e.getResult().equals(i_sword))
    81. {
    82. api.addExp(S, 50);
    83. }
    84. //Iron End
    85.  
    86. //Diamond Recipes
    87. if(e.getResult().equals(d_helm))
    88. {
    89. api.addExp(S, 500);
    90. }
    91. if(e.getResult().equals(d_chest))
    92. {
    93. api.addExp(S, 800);
    94. }
    95. if(e.getResult().equals(d_legs))
    96. {
    97. api.addExp(S, 700);
    98. }
    99. if(e.getResult().equals(d_boots))
    100. {
    101. api.addExp(S, 400);
    102. }
    103. if(e.getResult().equals(d_sword))
    104. {
    105. api.addExp(S, 200);
    106. }
    107. //Diamond Recipes End
    108. }
    109.  
    110.  
    111. }
    112.  
    113. public void onSmelt(InventoryClickEvent e)
    114. {
    115. String S = "Levels.Smithing";
    116. ItemStack i = new ItemStack(Material.IRON_INGOT);
    117. ItemStack g = new ItemStack(Material.GOLD_INGOT);
    118. ItemStack d = new ItemStack(Material.DIAMOND);
    119. ItemStack em = new ItemStack(Material.EMERALD);
    120.  
    121. if(e.getResult().equals(i))
    122. {
    123. api.addExp(S, 20);
    124. }
    125. if(e.getResult().equals(g))
    126. {
    127. api.addExp(S, 50);
    128. }
    129. if(e.getResult().equals(d))
    130. {
    131. api.addExp(S, 100);
    132. }
    133. if(e.getResult().equals(em))
    134. {
    135. api.addExp(S, 250);
    136. }
    137.  
    138. }
    139.  
    140. }
     
  2. Offline

    rfsantos1996

    You created a infinite loop of "new" objects
     
  3. Offline

    Kassestral

    can you show me how to fix it?
     
  4. Offline

    MrSparkzz

    Kassestral
    It will keep looping through these two pieces of code
    Code:java
    1. public Mining(Plugin levels) // 4. gets called by the API class
    2. {
    3. this.Levels = levels;
    4. this.api = new API(levels); // 1. calls the API
    5. }


    Code:java
    1. public API(Plugin levels) // 2. gets called by the Mining class
    2. {
    3. this.Levels = levels;
    4. this.mining = new Skills.Mining(levels); // 3. calls the Mining class
    5. this.smithing = new Skills.Smithing(levels);
    6. }
     
  5. Offline

    ZeusAllMighty11

    Could use a singleton.
     
  6. Offline

    rfsantos1996

    Well, you just need to see what MrSparkzz said and try to close the loop (by keeping instances of your classes instead of creating "new" things everytime)
     
  7. Offline

    Kassestral

    I fixed it now, but now when I break a stone block it springs
    Code:
    Caused by: java.lang.NullPointerException
        at Skills.Mining.onBlockBreak(Mining.java:30) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_05]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_05]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_05]
        at java.lang.reflect.Method.invoke(Method.java:483) ~[?:1.8.0_05]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:292) ~[bukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        ... 15 more
    
    ZeusAllMighty11 rfsantos1996
     
  8. Offline

    Garris0n

    Keep all the instances in your main class and access them from the external classes.

    Also, it's nice to see that all your variables/methods are readable.

    Your packages, however, seem a bit odd. I'd recommend checking out the Java documentation on packages.
     
  9. Offline

    rfsantos1996

    Can you send us the line 30 of the file Mining.java? (also, there is a thread somewhere about how to read stack trace, it is VERY useful (; )
     
  10. Offline

    Kassestral

  11. Offline

    rfsantos1996

    Maybe API is null... I don't see any other cause besides that, the addExp/setExp don't seem able to throw any error
     
  12. Offline

    Garris0n

    API is null because you never actually set it, which he doesn't know because you never posted the code here.
     
  13. Offline

    Kassestral

  14. Offline

    MrSparkzz

    Kassestral
    Change
    Code:java
    1. API api;
    2. public API getAPI(){
    3. return api;
    4. }

    to
    Code:java
    1. API api = API.getAPI();
     
  15. Offline

    Garris0n

    I said to create instance variables for all of the classes and put them in the main class along with a getter for each one.

    What you did was to put instance variables of the API class into every other class along with a getter.

    Telling him to use static singletons probably isn't the best idea.

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

    Kassestral

  17. Offline

    MrSparkzz

    Garris0n
    I don't see why not. It works, and if he runs into an error, there can be a fix. And I don't know why you wouldn't use the way I said. Why do you say that it isn't the best idea?

    Kassestral
    Do what I said, if you run into more problems, let me or someone else know, unless you can solve the problem on your own.
     
  18. Offline

    Garris0n

    Because he barely seems to understand how the instances work in the first place and using static singletons is pretty much guaranteed to throw that understanding off more.

    @OP, I'm just gonna leave this here and hope you read it, preferably before doing anything else.
    http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

    The entire section would probably be helpful, really.
    http://docs.oracle.com/javase/tutorial/java/javaOO/index.html
     
  19. Offline

    Kassestral

    I read it and I understand it allot more now then before
     
  20. Offline

    MrSparkzz

    Kassestral
    If your issue is resolved, please set this thread to solved.
     
  21. Offline

    Kassestral

    If anything I have made it worse, my onPlayerJoinEvent now has a null exception aswelll...
     
  22. Offline

    MrSparkzz

    Kassestral
    Show the new code of the class which contains the onPlayerJoinEvent and the console errors (stacktrace)
     
Thread Status:
Not open for further replies.

Share This Page