![]() |
|
[Java] Java "Null pointer Exception" (Bukkit) - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] Java "Null pointer Exception" (Bukkit) (/showthread.php?tid=4512286) |
Java "Null pointer Exception" (Bukkit) - Bradlant™ - Oct 28, 2014 Hello, So I just started making my own plugin with little knowledge of Java. To me this code looks right. When I have the item in my hand, it will take it, and will give me a coin (Once I tell it to...). But when I have the wrong item or no item in my hand, it gives me the null pointer exception error. Thank you. Code: Code @Override* - -Base- - Oct 28, 2014 White line is the error on so that I can try and determine what is null? * - Bradlant™ - Oct 28, 2014 (Oct 28, 2014 08:07 PM)-Base- Wrote: White line is the error on so that I can try and determine what is null? Sorry, that would be "if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED + "blood")) {" * - -Base- - Oct 28, 2014 It is probably the getItemInHand() method but could be getItemMeta(). Print out what getItemInHand() returns. It'll print null if it's null Oh just reread the post. It throws null when you don't have an item in your hand because well, you don't have an item in your hand. So you need to check if theirs an item in your hand before going on Oh just reread the post. It throws null when you don't have an item in your hand because well, you don't have an item in your hand. So you need to check if theirs an item in your hand before going on * - Corios - Oct 28, 2014 What IDE are you in? Most if you put your mouse over each method name, it will tell you what part is actually returning null * - Bradlant™ - Oct 28, 2014 (Oct 28, 2014 09:18 PM)-Base- Wrote: It is probably the getItemInHand() method but could be getItemMeta(). Print out what getItemInHand() returns. It'll print null if it's null Damn, that didn't work. Sounds like we're getting closer though. * - Freack100 - Oct 29, 2014 (Oct 28, 2014 10:08 PM)Fapster™ Wrote: Damn, that didn't work. Sounds like we're getting closer though. Check if there is an item in the hand, check if the item got ItemMeta and then check if the ItemMeta got a display name. If so, you can get the name of the item. * - MrOrang - Oct 29, 2014 Be sure to add a null check to getItemInHand() as the player could have no item in his hand which would mean it would return null. * - FormerlyS - Oct 29, 2014 if(player.getItemInHand != null){ //Now there's your line: if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED + "blood")) { //Do the usual stuff } } nullPointerException means it points to null. So this should do it. Otherwise you could do if(player.getItemInHand != null){ if(player.getItemInHand.getItemMeta() != null){ //Do your stuff } } Hope I helped 🤝 * - Bradlant™ - Oct 29, 2014 Thanks to all of you for helping me out! I took in all of your advice and ended up just re-writing the command. I went from Code if(player.getInventory().getItemInHand() != null) {To Code if(cmd.getName().equalsIgnoreCase("buyanarchycoin")) {* - whitewoolyhat - Nov 6, 2014 Presumably your class is implementing the CommandExecutor interface hence you have an @Override annotation? If so, then the line checking the commands name is useless because after you get the command and set the executor whenever the method is called upon it will recognise the command name. If you ever need help contact me, I have been learning java taught classes and bukkit for 4 years now and have worked for lots of big minecraft networks on massive paid projects. So yeah, this is "right down my street"! :) |