Oct 28, 2014 07:39 PM
(This post was last modified: Oct 28, 2014 08:46 PM by Bradlant™.)
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
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
Player player = (Player) sender;
if(cmd.getName().equalsIgnoreCase("buyanarchycoin")) {
if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED + "blood")) { //THIS LINE IS CAUSING THE PROBLEM.
if((player.getInventory().getItemInHand().getAmount() >= 64)) {
player.setItemInHand(null);
player.sendMessage("You have purchased 1 Anarchy Coin!");
}else{
player.sendMessage("You need 64 Blood to buy an Anarchy Coin!");
}
}else{
player.sendMessage("You must hold the blood in your hand!");
}
}
return false;
}
Very new, and still in Beta! Please leave feedback if you'd like to help me improve.
White line is the error on so that I can try and determine what is null?
Oct 28, 2014 08:46 PM
(This post was last modified: Oct 28, 2014 08:46 PM by Bradlant™.)
(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")) {"
Very new, and still in Beta! Please leave feedback if you'd like to help me improve.
Oct 28, 2014 09:18 PM
(This post was last modified: Oct 28, 2014 09:19 PM by -Base-.)
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
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
Oct 28, 2014 10:08 PM
(This post was last modified: Oct 28, 2014 10:16 PM by Bradlant™.)
(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
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
Damn, that didn't work. Sounds like we're getting closer though.
Very new, and still in Beta! Please leave feedback if you'd like to help me improve.
DO NOT DEAL WITH THIS MEMBER
(Oct 28, 2014 10:08 PM)Bradlant™ 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.
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.
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 🤝
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) {
if(player.getItemInHand().getItemMeta() != null) {
if(player.getItemInHand().getItemMeta().hasCustomName()) {
if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("blood")) {
if(player.getItemInHand().getAmount() == 64) {
player.sendMessage("You have purchased 1 Anarchy Coin!");
player.setItemInHand(null);
return true;
}else{
player.sendMessage("You need 64 Blood to buy an Anarchy Coin!");
return true;
}
}else{
player.sendMessage("You need to hold the blood in your hand!");
}
}else{
player.sendMessage("You need to hold the blood in your hand!");
}
}else{
player.sendMessage("You need to hold the blood in your hand!");
}
}
else if(player.getInventory().getItemInHand() == null){
player.sendMessage("You need to hold the blood in your hand!");
}
}
To
Code if(cmd.getName().equalsIgnoreCase("buyanarchycoin")) {
if(player.getInventory().getItemInHand().getType() == Material.INK_SACK) {
if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED + "Blood")) {
if(player.getItemInHand().getAmount() == 64) {
player.sendMessage("You have purchased 1 Anarchy Coin!");
player.setItemInHand(null);
return true;
}else{
player.sendMessage("You need 64 Blood to buy an Anarchy Coin!");
return true;
}
}else{
player.sendMessage("You need to hold the blood in your hand!");
return true;
}
}else{
player.sendMessage("You need to hold the blood in your hand!");
}
}
return false;
}
Very new, and still in Beta! Please leave feedback if you'd like to help me improve.
Nov 6, 2014 02:07 PM
(This post was last modified: Nov 6, 2014 02:09 PM by whitewoolyhat.)
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"! :)
|