Need help with NullPointerException

Discussion in 'Plugin Development' started by Scyfi, May 11, 2011.

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

    Scyfi

    I am trying to write my first mod and getting null exceptions. I am not sure where it is coming from but I guess has to do with how I am storing the data in the HashMap. Was hoping if you had a moment you could look over my code and maybe point me in the right direction.

    Overview

    I am trying to make a Remote Detonation Mod for TNT.
    A player would equip a button in their hand and then left click the tnt to link the button to the block of tnt.
    Then at a safe distance the player could left click again with the button to set off the TNT.

    The enabling of the button works ok as far as I can tell but when I left click anything it gives this error in the console.

    The enables seem to work ok and I get the messages fine, cant get the events to work properly. Here is the code to my two classes.

    Listener
    Show Spoiler
    Code:
    package org.scyfi.RemoteDet;
    
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerListener;
    
    public class RDListener extends PlayerListener{
    
        public RDListener(){
        }
    
        public void onPlayerInteract(PlayerInteractEvent event){
    
            Player player = event.getPlayer();
            Block block = event.getClickedBlock();
    
            if (block.getType() == Material.TNT){
                if(RemoteDet.isRemoteEnabled(player)){
                    if(event.getAction() == Action.LEFT_CLICK_BLOCK && player.getItemInHand().getType() == Material.STONE_BUTTON){
                        RemoteDet.armRemote(player, block);
                        }
                    else if(event.getAction() == Action.LEFT_CLICK_AIR && player.getItemInHand().getType() == Material.STONE_BUTTON && RemoteDet.isRemoteArmed(player)){
                        RemoteDet.detonateTNT(player);
                    }
                }
            }
        }
    
    }

    Main class
    Show Spoiler

    Code:
    package org.scyfi.RemoteDet;
    
    import java.util.HashMap;
    import java.util.logging.Logger;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Event;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.TNTPrimed;
    import org.bukkit.event.Event.Priority;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class RemoteDet extends JavaPlugin{
    
        public final Logger log = Logger.getLogger("Minecraft");
        public final String pluginName = "Remote Det";
    
        private RDListener blockListener;
        private static TNTPrimed tnt;
    
        private static HashMap<Player, Boolean> remoteEnabled = new HashMap<Player, Boolean>();
        private static HashMap<Player, Block> armedRemote = new HashMap<Player, Block>();
    
        public void onEnable(){
            log.info(pluginName + " - Version " + this.getDescription().getVersion() + " Enabled");
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvent(Event.Type.PLAYER_INTERACT, blockListener, Priority.Normal, this);
            blockListener = new RDListener();
    
        }
    
        public void onDisable(){
            log.info(pluginName + " - Version " + this.getDescription().getVersion() + " has been disabled.");
    
        }
    
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase("rd")) {
                if(sender instanceof Player) {
                    toggleRemote((Player)sender);
                }
                return true;
            }
            return false;
        }
    
        public void toggleRemote(Player player){
            if(remoteEnabled.containsKey(player)){
                if(remoteEnabled.get(player)){
                    remoteEnabled.put(player, true);
                    player.sendMessage(ChatColor.YELLOW + "Remote Detonator enabled!");
                } else {
                    remoteEnabled.put(player, true);
                    player.sendMessage(ChatColor.YELLOW + "Remote Detonator disabled!");
                }
            } else {
                remoteEnabled.put(player, false);
                player.sendMessage(ChatColor.YELLOW + "Remote Detonator enabled!");
            }
        }
    
        public static boolean isRemoteEnabled(Player player){
            return (remoteEnabled.get(player)).booleanValue();
        }
    
        public static void armRemote(Player player, Block block){
                armedRemote.put(player, block);
                player.sendMessage(ChatColor.RED + "Remote is ARMED!!");
        }
    
        public static boolean isRemoteArmed(Player player){
            return(armedRemote.containsKey(player));
        }
    
        public static void detonateTNT(Player player) {
            tnt.teleport((armedRemote.get(player)).getLocation());
            (armedRemote.get(player)).setType(Material.AIR);
        }
    }
    


    I am pretty new at this so I am sure it is something stupid I am missing. Thanx for your time.
     
  2. Offline

    Kitteh

    if the hashmap returns null (no key) you can't return a boolean value from it I believe. Also I have no idea why you are using static methods in your main plugin file when the information is required by the listener?
     
  3. Offline

    MSC

    Code:
            pm.registerEvent(Event.Type.PLAYER_INTERACT, blockListener, Priority.Normal, this);
            blockListener = new RDListener();
    you got those backwards. :D
     
  4. Offline

    Scyfi

    I removed the static field from the methods. I also when in and added if statement for my hashmaps checks to look for presence of a key first. Still getting the same errors.

    haha I hate myself.... :mad:

    Still getting an error but at least now it is giving me line numbers to work with. Thank you much!

    Still getting NPE's from what @Kitteh was saying with my hashmap checks. Is there an easier way to do this then using Hashmap? Just use an arraylist or something?

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

    MSC

    What you're running into specifically is the fact that isRemoteEnabled is calling .booleanValue() on a pointer that may be null. You can fix it by checking for that first. Although really, instead of using a HashMap<Player,Boolean> like you are, I'd reccomend using a LinkedList<Player> and then returning remoteEnabled.contains(). Here's some code that should work:
    Code:
    public class RemoteDet extends JavaPlugin{
    
        // ...
    
        private static List<Player> remoteEnabled = new LinkedList<Player>();
        private static HashMap<Player, Block> armedRemote = new HashMap<Player, Block>();
    
        // ...
    
        public static void toggleRemote(Player player){
        	if(remoteEnabled.contains(player)){
                remoteEnabled.remove(player);
                player.sendMessage(ChatColor.YELLOW + "Remote Detonator disabled!");
        	} else {
        	    remoteEnabled.add(player);
                player.sendMessage(ChatColor.YELLOW + "Remote Detonator enabled!");
        	}
        }
    
        public static boolean isRemoteEnabled(Player player){
            return remoteEnabled.contains(player);
        }
        
        // ...
        
    }
     
  6. Offline

    Scyfi

    @Kitteh @MSC

    I finally got it all working and would like to thank both of you for the help.

    Source
     
Thread Status:
Not open for further replies.

Share This Page