Can someone explain to me why this doesn't work?

Discussion in 'Plugin Development' started by samus1221, Apr 20, 2014.

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

    samus1221

    Alright so I am trying to make a bukkit runnable that cycles through the online players to check whether or not they are wearing a certain type of armor. It works when I have it set to like leather boots but when I take them off it says there is something wrong with the if statement. It makes no sense, if someone can iterate what is going on you will be my hero. I thank you in advance.

    Code:
    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new BukkitRunnable() {
                    public void run() {
                        for(Player player : Bukkit.getOnlinePlayers()){
                            PlayerInventory pi = player.getInventory();
                            if(pi.getBoots().getType() == (Material.AIR)){
                                player.sendMessage("Success!");
                            }else{
                                player.sendMessage("Failure!");
                            }
                        }
                    }
                }, 20, 100);
     
  2. Offline

    MOMOTHEREAL

    What is the problem? Is it a console error or an error in your IDE?
     
  3. Offline

    adam753

    The code seems fine. Can you post the error message you get when it "says there is something wrong with the if statement"?
     
  4. Offline

    samus1221

    It is a console error, a nullpointerexception although I have no idea what could be null.

    20.04 16:54:28 [Server] INFO at net.minecraft.server.v1_7_R2.ThreadServerApplication.run(SourceFile:618) [CraftBukkit-1.7.5-Dev.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
    20.04 16:54:28 [Server] INFO at net.minecraft.server.v1_7_R2.MinecraftServer.run(MinecraftServer.java:459) [CraftBukkit-1.7.5-Dev.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
    20.04 16:54:28 [Server] INFO at net.minecraft.server.v1_7_R2.MinecraftServer.u(MinecraftServer.java:548) [CraftBukkit-1.7.5-Dev.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
    20.04 16:54:28 [Server] INFO at net.minecraft.server.v1_7_R2.DedicatedServer.v(DedicatedServer.java:250) [CraftBukkit-1.7.5-Dev.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
    20.04 16:54:28 [Server] INFO at net.minecraft.server.v1_7_R2.MinecraftServer.v(MinecraftServer.java:590) [CraftBukkit-1.7.5-Dev.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
    20.04 16:54:28 [Server] INFO at org.bukkit.craftbukkit.v1_7_R2.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345) [CraftBukkit-1.7.5-Dev.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
    20.04 16:54:28 [Server] INFO at org.bukkit.craftbukkit.v1_7_R2.scheduler.CraftTask.run(CraftTask.java:53) ~[CraftBukkit-1.7.5-Dev.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
    20.04 16:54:28 [Server] INFO at net.example.example.example$1.run(example.java:46) ~[?:?]
    20.04 16:54:28 [Server] INFO java.lang.NullPointerException

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

    malandrix_bunny

    Is that the full stacktrace?
     
  6. Offline

    bobacadodl

    Its erroring because of this line
    Code:
    if(pi.getBoots().getType() == (Material.AIR)){
    I'm guessing this is because if you have your boots off, then pi.getBoots() returns null. You can't call the method getType() on a null object, so it causes an error.

    To fix this, add a check to see if pi.getBoots() != null before that statement
     
  7. Offline

    adam753

    samus1221
    Perhaps getBoots() is returning null?
     
  8. Offline

    samus1221

    Ohhh thank you guys, I appreciate it that was the problem.

    Actually that still did not fix the problem, here is the actual code:
    Code:
    if(pi.getBoots() != null || pi.getChestplate() != null || pi.getLeggings() != null || pi.getHelmet() != null){
                           
                            }else{
                                if(pi.getBoots().getType().equals(Material.LEATHER_BOOTS) || pi.getChestplate().getType().equals(Material.LEATHER_CHESTPLATE) || pi.getLeggings().getType().equals(Material.LEATHER_LEGGINGS) || pi.getHelmet().getType().equals(Material.LEATHER_HELMET)){
                                    if(blockDeterminer.getPlayerRank(player).equalsIgnoreCase("D")){
                                       
                                    }else{
                                       
                                    }
                                }
                                else if(pi.getBoots().getType().equals(Material.CHAINMAIL_BOOTS) || pi.getChestplate().getType().equals(Material.CHAINMAIL_CHESTPLATE) || pi.getLeggings().getType().equals(Material.CHAINMAIL_LEGGINGS) || pi.getHelmet().getType().equals(Material.CHAINMAIL_HELMET)){
                                    if(blockDeterminer.getPlayerRank(player).equalsIgnoreCase("C")){
                                       
                                    }else{
                                        if(blockDeterminer.getNextBlock(player).equalsIgnoreCase("C")){
                                            handler.giveBook(player);
                                        }
                                    }
                                }
                                else if(pi.getBoots().getType().equals(Material.IRON_BOOTS) || pi.getChestplate().getType().equals(Material.IRON_CHESTPLATE) || pi.getLeggings().getType().equals(Material.IRON_LEGGINGS) || pi.getHelmet().getType().equals(Material.IRON_HELMET)){
                                    if(blockDeterminer.getPlayerRank(player).equalsIgnoreCase("B")){
                                       
                                    }else{
                                        if(blockDeterminer.getNextBlock(player).equalsIgnoreCase("B")){
                                            handler.giveBook(player);
                                        }
                                    }
                                }
                                else if(pi.getBoots().getType().equals(Material.DIAMOND_BOOTS) || pi.getChestplate().getType().equals(Material.DIAMOND_CHESTPLATE) || pi.getLeggings().getType().equals(Material.DIAMOND_LEGGINGS) || pi.getHelmet().getType().equals(Material.DIAMOND_HELMET)){
                                    if(blockDeterminer.getPlayerRank(player).equalsIgnoreCase("A")){
                                       
                                    }else{
                                        if(blockDeterminer.getNextBlock(player).equalsIgnoreCase("A")){
                                            handler.giveBook(player);
                                        }
                                    }
                                }
                            }
    I am starting to get the error when it checks if it is a specific armor set.

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

    AoH_Ruthless

    samus1221
    First of all, why do you have empty if and else statements? That's just strange...

    You do realize that all 4 of your statements could work. If I had a diamond helmet, iron chestplate, chain legs and leather boots all 4 of the snippets would execute.

    What error are you getting on the armor set check?
     
  10. Offline

    Garris0n

    Note that getArmorContents, unlike pretty much everything else in Bukkit, returns empty ItemStacks instead of null ones. It's because it actually returns an ItemStack with a null NMS handle instead of just returning null.
     
    bobacadodl likes this.
  11. Offline

    bobacadodl

    Just wondering, is there an actual purpose to this? Why doesnt it just simply return null?
     
  12. Offline

    Garris0n

    Well in CraftInventory they did it properly. It has a check for a null item and returns null if so. In CraftPlayerInventory, it simply returns an item regardless of what the NMS item is. And an ItemStack with a null NMS ItemStack essentially functions as air. So, from what I can tell, there is no purpose. Somebody just forgot a null check.
     
    bobacadodl likes this.
  13. Offline

    samus1221

    Alright, so i started using the getArmorContents, I checked to see if it was null. But I still get an error regarding whether or not they have leather armor on, and so forth. I just don't understand why it is giving me a nullpointer.
     
  14. Offline

    Garris0n

    Post the full class and error.
     
  15. Offline

    samus1221

    The Class

    Code:java
    1. package net.example.example;
    2.  
    3. import java.sql.SQLException;
    4.  
    5. import net.example.example.events.PlayerJoinEventListener;
    6. import net.example.example.items.ItemHandler;
    7. import net.example.example.methods.blockDeterminer;
    8. import net.example.example.mysql.MySQLMethods;
    9. import net.milkbowl.vault.economy.Economy;
    10.  
    11. import org.bukkit.Bukkit;
    12. import org.bukkit.Material;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.inventory.PlayerInventory;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16. import org.bukkit.scheduler.BukkitRunnable;
    17.  
    18. public class Example extends JavaPlugin{
    19.  
    20. private static Example instance;
    21. private static Economy econ = null;
    22.  
    23. //Gives access to main class
    24. public static Example getInstance(){
    25. return instance;
    26. }
    27.  
    28.  
    29. @Override
    30. public void onEnable(){
    31. if(this.getServer().getPluginManager().getPlugin("Vault") == null){
    32. this.getServer().getPluginManager().disablePlugin(this);
    33. this.getLogger().info("Plugin disabled due to Vault not being found!");
    34. }else{
    35. this.getServer().getPluginManager();
    36. instance = this;
    37. PlayerJoinEventListener joinEvent = new PlayerJoinEventListener(this);
    38. Bukkit.getServer().getPluginManager().registerEvents(joinEvent, this);
    39. getLogger().info("has been enabled!");
    40. final ItemHandler handler = new ItemHandler();
    41. final blockDeterminer blockDeterminer = new blockDeterminer();
    42. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new BukkitRunnable() {
    43. public void run() {
    44. for(Player player : Bukkit.getOnlinePlayers()){
    45. PlayerInventory pi = player.getInventory();
    46. if(pi.getArmorContents() == null){
    47.  
    48. }else{
    49. if(pi.getBoots().getType().equals(Material.LEATHER_BOOTS) ){
    50. if(blockDeterminer.getPlayerRank(player).equalsIgnoreCase("D")){
    51.  
    52. }else{
    53.  
    54. }
    55. }
    56. else if(pi.getBoots().getType().equals(Material.CHAINMAIL_BOOTS) || pi.getChestplate().getType().equals(Material.CHAINMAIL_CHESTPLATE) || pi.getLeggings().getType().equals(Material.CHAINMAIL_LEGGINGS) || pi.getHelmet().getType().equals(Material.CHAINMAIL_HELMET)){
    57. if(blockDeterminer.getPlayerRank(player).equalsIgnoreCase("C")){
    58.  
    59. }else{
    60. if(blockDeterminer.getNextBlock(player).equalsIgnoreCase("C")){
    61. handler.giveBook(player);
    62. }
    63. }
    64. }
    65. else if(pi.getBoots().getType().equals(Material.IRON_BOOTS) || pi.getChestplate().getType().equals(Material.IRON_CHESTPLATE) || pi.getLeggings().getType().equals(Material.IRON_LEGGINGS) || pi.getHelmet().getType().equals(Material.IRON_HELMET)){
    66. if(blockDeterminer.getPlayerRank(player).equalsIgnoreCase("B")){
    67.  
    68. }else{
    69. if(blockDeterminer.getNextBlock(player).equalsIgnoreCase("B")){
    70. handler.giveBook(player);
    71. }
    72. }
    73. }
    74. else if(pi.getBoots().getType().equals(Material.DIAMOND_BOOTS) || pi.getChestplate().getType().equals(Material.DIAMOND_CHESTPLATE) || pi.getLeggings().getType().equals(Material.DIAMOND_LEGGINGS) || pi.getHelmet().getType().equals(Material.DIAMOND_HELMET)){
    75. if(blockDeterminer.getPlayerRank(player).equalsIgnoreCase("A")){
    76.  
    77. }else{
    78. if(blockDeterminer.getNextBlock(player).equalsIgnoreCase("A")){
    79. handler.giveBook(player);
    80. }
    81. }
    82. }
    83. }
    84.  
    85. }
    86. }
    87. }, 20, 100);
    88. }
    89. }
    90.  
    91. @Override
    92. public void onDisable(){
    93. try{
    94. if(MySQLMethods.connection != null && !MySQLMethods.connection.isClosed()){
    95. MySQLMethods.connection.close();
    96. }
    97. }catch (SQLException e){
    98. e.printStackTrace();
    99. }
    100. getLogger().info("has been disabled!");
    101. }
    102.  
    103.  
    104. }



    The Error

    Code:
    [09:40:21 WARN]: [Example] Task #27 for Example v0.0.1 generated an exception
    java.lang.NullPointerException
        at net.example.example.Example$1.run(Example.java:49) ~[?:?]
        at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftTask.run(CraftTask.java:53) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:587) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    
    I posted it above.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  16. samus1221
    Please use Java syntax.

    samus1221
    Use [ syntax=java ] [ /syntax ] BB- code because i dont want to count the rows

    Code:java
    1. if(pi.getBoots().getType().equals(Material.LEATHER_BOOTS) ){
    this line throws nullpointerexception.

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

    samus1221

    I know that.

    I don't understand why it throws it.

    I apologize for snapping like that, didn't really know what you were talking about.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  18. Try to put this line above it:
    Code:java
    1. if(pi.getBoots().getType() == null ){}
     
  19. Offline

    samus1221

    Instead of getting that code working, I will rewrite it. How do you think I should go about checking if they are wearing certain types of armor?
     
  20. Offline

    Garris0n

    getBoots() == null, not boots.getType()
     
  21. Offline

    samus1221

    Can I do something to the extent of getArmorContents and checking whether it is of a certain material?
     
  22. Offline

    Garris0n

    getArmorContents returns all the armor. You need to compare each piece of armor against whatever material you want.
     
  23. Offline

    Minnymin3

    Check to make sure that each piece of armour does not equal null.
    You are not doing this in your latest posted code. You are just checking the armour contents.

    Add player.getBoots() == null to your player.getArmorContents() == null section
     
Thread Status:
Not open for further replies.

Share This Page