Solved Config file Null Pointer Exception

Discussion in 'Plugin Development' started by bobthefish, Jun 3, 2014.

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

    bobthefish

    Hi guys, as the title says, my code for my plugin is getting a null pointer exception.

    here the class involved, and the error in the console:
    Code:java
    1. package me.BobTheFish.Kingdoms;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.player.PlayerJoinEvent;
    8. import org.bukkit.inventory.ItemStack;
    9.  
    10. public class JoinListener implements Listener{
    11.  
    12. mainKingdoms plugin;
    13. public JoinListener(mainKingdoms plugin) {
    14. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    15. }
    16.  
    17. @EventHandler
    18. public void onJoin(PlayerJoinEvent join){
    19. Player p = join.getPlayer();
    20. p.sendMessage(p.getName());
    21. p.sendMessage("yes");
    22. boolean c = plugin.getConfig().isSet("Players");
    23. System.out.print(c);
    24. if(!plugin.getConfig().isSet("Players." + p.getName() + ".team")){
    25. p.sendMessage("yes");
    26. plugin.getConfig().set("Players." + p.getName() + ".team", "n");
    27. p.sendMessage("yes");
    28. }
    29. p.getInventory().addItem(new ItemStack(Material.CLAY_BALL, 1));
    30. }

    The boolean on line 22 was just a test to determine the problem, the problem is in that line.
    Code:
    [17:37:41 INFO]: UUID of player AdmiralA01 is adc6979f4d084c029d999c01ff43d149
    [17:37:41 INFO]: AdmiralA01[/96.244.244.6:1025] logged in with entity id 33317 at ([world] 1.6575501474479957, 90.0, 198.49826300747927)
    [17:37:41 ERROR]: Could not pass event PlayerJoinEvent to Kingdoms version Pre-Alpha-Alpha 1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[server.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[server.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:486) [server.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:471) [server.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PlayerList.c(PlayerList.java:225) [server.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PlayerList.a(PlayerList.java:116) [server.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.LoginListener.c(LoginListener.java:78) [server.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.LoginListener.a(LoginListener.java:42) [server.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:149) [server.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [server.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [server.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [server.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [server.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [server.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [server.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.NullPointerException
        at me.BobTheFish.Kingdoms.JoinListener.onJoin(JoinListener.java:22) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_17]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_17]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_17]
        at java.lang.reflect.Method.invoke(Method.java:601) ~[?:1.7.0_17]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:318) ~[server.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        ... 14 more
     
  2. Offline

    1Rogue

    You never set the value of the "plugin" variable in your constructor.
     
  3. Offline

    bobthefish

    1Rogue I thought that plugin was the mainKindoms, two lines before? But i could be wrong, I just looked at a tutorial really quick on using listeners in different classes
     
  4. Offline

    1Rogue


    Code:java
    1. mainKingdoms plugin;


    Right now, this is simply a variable. It does not have a reference to anything at all, so its reference is considered "null". When you attempt to call a method on a variable, it needs to take the variable's reference and then point to the appropriate instance in order to execute something. This works fine, until you don't have anywhere to point.

    You need to set the variable's value in your constructor with the passed value.

    Code:java
    1. this.plugin = plugin;
     
    bobthefish likes this.
  5. Offline

    bobthefish

    1Rogue so

    Code:java
    1. mainKingdoms plugin;
    2. public JoinListener(mainKingdoms plugin) {
    3. plugin.getServer().getPluginManager().registerEvents(this, this.plugin = plugin);
    4. }
    ??
     
  6. Offline

    1Rogue

    Considering that very likely won't compile, no.
    Set the variable's value in a separate statement
     
    bobthefish likes this.
  7. Offline

    bobthefish

    1Rogue I just tried it, it did compile and it did fix the error, but I see you suggest something different, how would I put that in a different argument?
    Code:java
    1. mainKingdoms plugin;
    2. public JoinListener(mainKingdoms plugin) {
    3. this.plugin = plugin;
    4. plugin.getServer().getPluginManager().registerEvents(this, this.plugin);
    5. }


    like that??
     
  8. Offline

    1Rogue


    Exactly!
     
  9. Offline

    bobthefish

    1Rogue ok thanks so much for the help!!!!!!!!!

    Ok, I ran into another related problem, but this one isnt in a listener class, so where do I put it? here is the problem (another NPE)
    Code:java
    1. public String getTeam(Player p){
    2. return plugin.getConfig().getString("Players." + p.getName() + ".team");
    3. }

    the error is on line 2
    Im guessing same problem, so where do I put the this.plugin = plugin? also the mainKingdom plugin; is static if that matters

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

    1Rogue


    It doesn't need to be static, and you simply place it in your constructor again.
     
  11. Offline

    bobthefish

    1Rogue maybe im doing something very wrong, but I dont have a constructor for this class

    and it does need to be static because all of the other methods that use the config in the class are static, theyre static because when I reference them in another class, eclipse told me that those methods had to be static
     
  12. Offline

    fireblast709

    bobthefish then don't make your methods static either? Just create an instance of the class in your main class, and add a getter method to access that instance through the instance of the main class.
     
    1Rogue likes this.
  13. Offline

    bobthefish

    This is my Join class. this will make a player join a team whenever they click on an inventory spot.

    Show Spoiler

    Code:java
    1. public class InventoryMaker implements Listener{
    2.  
    3.  
    4. public InventoryMaker(mainKingdoms plugin) {
    5. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    6. }
    7. public static Inventory myInventory = Bukkit.createInventory(null, 9, "Choose a Kingdom!");
    8. // The first parameter, is the inventory owner. I make it null to let everyone use it.
    9. //The second parameter, is the slots in a inventory. Must be a multiple of 9. Can be up to 54.
    10. //The third parameter, is the inventory name. This will accept chat colors
    11. static {
    12.  
    13. ItemStack m = new ItemStack(maker(Material.STAINED_GLASS_PANE,1,10,"The Dynasty","Courageous", "Darteol", ChatColor.DARK_PURPLE,ChatColor.GOLD));
    14. ItemStack q = new ItemStack(maker(Material.STAINED_GLASS_PANE,1,13,"Eldin","Fearless", "Battlecat30", ChatColor.DARK_GREEN,ChatColor.DARK_AQUA));
    15. ItemStack w = new ItemStack(maker(Material.STAINED_GLASS_PANE,1,13,"Camelot","Passionate", "AdmiralA01", ChatColor.DARK_GREEN,ChatColor.GOLD));
    16. ItemStack e = new ItemStack(maker3(Material.STAINED_GLASS_PANE,1,10,"The Kingdom of Rexro","Strategic", "R3DC0DE137","nalapups2002", ChatColor.DARK_PURPLE,ChatColor.DARK_RED));
    17. ItemStack r = new ItemStack(maker(Material.STAINED_GLASS_PANE,1,5,"Fort Lockwood","Inspiring", "Darteol", ChatColor.GREEN,ChatColor.DARK_GREEN));
    18. ItemStack t = new ItemStack(maker(Material.STAINED_GLASS_PANE,1,7,"Ruins of the Ancients","Decisive", "Darteol", ChatColor.DARK_GRAY,ChatColor.AQUA));
    19. ItemStack y = new ItemStack(maker3(Material.STAINED_GLASS_PANE,1,14,"Tahula Solaire","Skilled", "ethanburda1","BladedDingo", ChatColor.DARK_RED,ChatColor.GOLD));
    20. ItemStack u = new ItemStack(maker2(Material.STAINED_GLASS_PANE,1,1,"YorkShire","Responsible", "christina___nyc", ChatColor.GOLD,ChatColor.DARK_GRAY));
    21. ItemStack i = new ItemStack(maker(Material.STAINED_GLASS_PANE,1,14,"Mordor","Fearsome", "FrightKnight39", ChatColor.DARK_RED,ChatColor.DARK_GRAY));
    22.  
    23.  
    24. myInventory.setItem(0,m);
    25. myInventory.setItem(1,q);
    26. myInventory.setItem(2,w);
    27. myInventory.setItem(3,e);
    28. myInventory.setItem(4,r);
    29. myInventory.setItem(5,t);
    30. myInventory.setItem(6,y);
    31. myInventory.setItem(7,u);
    32. myInventory.setItem(8,i);
    33. }
    34.  
    35. @EventHandler
    36. public void onInventoryClick(InventoryClickEvent event) {
    37. Player player = (Player) event.getWhoClicked(); // The player that clicked the item
    38. Inventory inventory = event.getInventory(); // The inventory that was clicked in
    39.  
    40. if (inventory.getName().equals(myInventory.getName()))
    41. {
    42. if (event.getSlot() == 0) {
    43. event.setCancelled(true);
    44. player.closeInventory();
    45. Join.Dynasty(player); //right here is tells me that "Cannot make a static reference to the non-static method Dynasty(Player) from the type Join"
    46. }
    47. else if (event.getSlot() == 1) {
    48. event.setCancelled(true);
    49. player.closeInventory();
    50. Join.Eldin(player); // and here
    51. }
    52. else if (event.getSlot() == 2) {
    53. event.setCancelled(true);
    54. player.closeInventory();
    55. Join.Camelot(player); //and here... and so on
    56. }
    57. else if (event.getSlot() == 3) {
    58. event.setCancelled(true);
    59. player.closeInventory();
    60. Join.Rexro(player);
    61. }
    62. else if (event.getSlot() == 4) {
    63. event.setCancelled(true);
    64. player.closeInventory();
    65. Join.FortLock(player);
    66. }
    67. else if (event.getSlot() == 5) {
    68. event.setCancelled(true);
    69. player.closeInventory();
    70. Join.RuinsAncient(player);
    71. }
    72. else if (event.getSlot() == 6) {
    73. event.setCancelled(true);
    74. player.closeInventory();
    75. Join.Tahula(player);
    76. }
    77. else if (event.getSlot() == 7) {
    78. event.setCancelled(true);
    79. player.closeInventory();
    80. Join.YorkShire(player);
    81. }
    82. else if (event.getSlot() == 8) {
    83. event.setCancelled(true);
    84. player.closeInventory();
    85. Join.Mordor(player);
    86. }
    87. else{
    88. player.sendMessage("no work");
    89. }
    90. }
    91. }
    92. public static ItemStack maker(Material mat, int i, int n, String kingdomName, String adj, String name, ChatColor color, ChatColor color2){
    93. ItemStack it = new ItemStack(mat, i, (short) n);
    94. ItemMeta m = it.getItemMeta();
    95. ArrayList<String> s = new ArrayList<String>();
    96. m.setDisplayName(color + kingdomName);
    97. s.add(color2 + "" +ChatColor.ITALIC + "Led by the");
    98. s.add(color2 + "" +ChatColor.ITALIC + adj);
    99. s.add(color2 + "" +ChatColor.ITALIC + "King " + color + "" + ChatColor.ITALIC + name + color2 + "" + ChatColor.ITALIC + "!");
    100. m.setLore(s);
    101. it.setItemMeta(m);
    102. return it;
    103. }
    104. public static ItemStack maker2(Material mat, int i, int n, String kingdomName, String adj, String name, ChatColor color, ChatColor color2){
    105. ItemStack it = new ItemStack(mat, i, (short) n);
    106. ItemMeta m = it.getItemMeta();
    107. ArrayList<String> s = new ArrayList<String>();
    108. m.setDisplayName(color + kingdomName);
    109. s.add(color2 + "" +ChatColor.ITALIC + "Led by the");
    110. s.add(color2 + "" +ChatColor.ITALIC + adj);
    111. s.add(color2 + "" +ChatColor.ITALIC + "Queen " + color + "" + ChatColor.ITALIC + name + color2 + "" + ChatColor.ITALIC + "!");
    112. m.setLore(s);
    113. it.setItemMeta(m);
    114. return it;
    115. }
    116. public static ItemStack maker3(Material mat, int i, int n, String kingdomName, String adj, String name, String coname, ChatColor color, ChatColor color2){
    117. ItemStack it = new ItemStack(mat, i, (short) n);
    118. ItemMeta m = it.getItemMeta();
    119. ArrayList<String> s = new ArrayList<String>();
    120. m.setDisplayName(color + kingdomName);
    121. s.add(color2 + "" +ChatColor.ITALIC + "Led by the");
    122. s.add(color2 + "" +ChatColor.ITALIC + adj);
    123. s.add(color2 + "" +ChatColor.ITALIC + "King " + color + "" + ChatColor.ITALIC + name);
    124. s.add(color2+ "" +ChatColor.ITALIC + "and Co-led by ");
    125. s.add(color + "" + ChatColor.ITALIC + coname + color2 + ChatColor.ITALIC + "!");
    126. m.setLore(s);
    127. it.setItemMeta(m);
    128. return it;
    129. }
    130.  


    I suppose that this could be the problem in there, but i dont know how to fix it..

    Code:java
    1. public static Inventory myInventory = Bukkit.createInventory(null, 9, "Choose a Kingdom!");
    2. static {
    3.  
    4. ItemStack m = new ItemStack(maker(Material.STAINED_GLASS_PANE,1,10,"The Dynasty","Courageous", "Darteol", ChatColor.DARK_PURPLE,ChatColor.GOLD));
    5. ItemStack q = new ItemStack(maker(Material.STAINED_GLASS_PANE,1,13,"Eldin","Fearless", "Battlecat30", ChatColor.DARK_GREEN,ChatColor.DARK_AQUA));
    6. ItemStack w = new ItemStack(maker(Material.STAINED_GLASS_PANE,1,13,"Camelot","Passionate", "AdmiralA01", ChatColor.DARK_GREEN,ChatColor.GOLD));
    7. ItemStack e = new ItemStack(maker3(Material.STAINED_GLASS_PANE,1,10,"The Kingdom of Rexro","Strategic", "R3DC0DE137","nalapups2002", ChatColor.DARK_PURPLE,ChatColor.DARK_RED));
    8. ItemStack r = new ItemStack(maker(Material.STAINED_GLASS_PANE,1,5,"Fort Lockwood","Inspiring", "Darteol", ChatColor.GREEN,ChatColor.DARK_GREEN));
    9. ItemStack t = new ItemStack(maker(Material.STAINED_GLASS_PANE,1,7,"Ruins of the Ancients","Decisive", "Darteol", ChatColor.DARK_GRAY,ChatColor.AQUA));
    10. ItemStack y = new ItemStack(maker3(Material.STAINED_GLASS_PANE,1,14,"Tahula Solaire","Skilled", "ethanburda1","BladedDingo", ChatColor.DARK_RED,ChatColor.GOLD));
    11. ItemStack u = new ItemStack(maker2(Material.STAINED_GLASS_PANE,1,1,"YorkShire","Responsible", "christina___nyc", ChatColor.GOLD,ChatColor.DARK_GRAY));
    12. ItemStack i = new ItemStack(maker(Material.STAINED_GLASS_PANE,1,14,"Mordor","Fearsome", "FrightKnight39", ChatColor.DARK_RED,ChatColor.DARK_GRAY));
    13.  
    14.  
    15. myInventory.setItem(0,m);
    16. myInventory.setItem(1,q);
    17. myInventory.setItem(2,w);
    18. myInventory.setItem(3,e);
    19. myInventory.setItem(4,r);
    20. myInventory.setItem(5,t);
    21. myInventory.setItem(6,y);
    22. myInventory.setItem(7,u);
    23. myInventory.setItem(8,i);
    24.  
    25. }

    if you look at the comments in there, you will see my problem. but also, fireblast709 your post kinda very much confused me, but I think I got it, so make a constructor in your class:

    Code:java
    1. public Join(mainKingdoms plugin){
    2. this.plugin = plugin;
    3. }


    and in your main put
    Code:java
    1. New Join(this);


    ????? thank you for all of the help so far
     
  14. Offline

    fireblast709

    bobthefish then save it as a member of mainKingdoms
    Code:java
    1. private Join join;
    2. ...
    3. public void onEnable()
    4. {
    5. // Assuming you make your object here. It could be anywhere, really
    6. this.join = new Join(this);
    7. }
    And add a getter method
    Code:java
    1. public Join getJoin()
    2. {
    3. return this.join;
    4. }
    Now you can do the following in InventoryMaker
    Code:java
    1. Join join = this.plugin.getJoin();
    Of course you still need to add mainKingdom as a field there :p
    Code:java
    1. private mainKingdoms plugin;
    2.  
    3. public InventoryMaker(mainKingdoms plugin)
    4. {
    5. this.plugin = plugin
    6. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    7. }
    Lastly I really like to stress to enforce the Java naming standard a bit more, CamelCase for your classes, just because it makes me feel itchy when looking at it: MainKingdoms instead of mainKingdoms :p.
     
Thread Status:
Not open for further replies.

Share This Page