NullPointerException! Why?

Discussion in 'Plugin Development' started by Treeline1, Jan 22, 2015.

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

    Treeline1

    This bug is supppperrr annoying! And I dont know why xD I've spent ages on this but I cannot figure out why it is Null! D;
    When I do the command /buyscout it gives me an error on this line: permission.playerAddTransient(player,"scout.set");
    It doesnt that for all of them. Can someone help? Thanks
    Code:
    package me.liam.main;
    
    
    
    import java.util.ArrayList;
    
    import me.liam.commands.ArcherCommand;
    import me.liam.commands.ChestCommand;
    import me.liam.commands.FishermanCommand;
    import me.liam.commands.GravitatorCommand;
    import me.liam.commands.HopperCommand;
    import me.liam.commands.JuggernautCommand;
    import me.liam.commands.KitCommand;
    import me.liam.commands.MarioCommand;
    import me.liam.commands.Menu;
    import me.liam.commands.PilotCommand;
    import me.liam.commands.RageCommand;
    import me.liam.commands.RemoverCommand;
    import me.liam.commands.ScoutCommand;
    import me.liam.commands.SniperCommand;
    import me.liam.commands.SoupCommand;
    import me.liam.commands.SpawnCommand;
    import me.liam.commands.SquashCommand;
    import me.liam.commands.SquidCommand;
    import me.liam.event.BlockListener;
    import me.liam.event.DamageListener;
    import me.liam.event.InventoryListener;
    import me.liam.event.KillstreakEvent;
    import me.liam.event.PlayerListener;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    import net.milkbowl.vault.permission.Permission;
    
    import org.bukkit.Bukkit;
    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.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Kitpvp extends JavaPlugin implements Listener{
    
        public static Economy econ = null;
        public static Kitpvp inst;
        public static Permission permission = null;
        private Menu menu;
    
        private ArrayList<Player> vanished = new ArrayList<Player>();
    
        private boolean setupPermissions() {
            RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
            permission = rsp.getProvider();
            return permission != null;
        }
    
    
    
        private PlayerListener Events = new PlayerListener(this);
        public ArrayList<String> KitCommand = new ArrayList<String>();
        public ArrayList<String> SniperCommand = new ArrayList<String>();
        public ArrayList<String> HopperCommand = new ArrayList<String>();
        public ArrayList<String> RageCommand = new ArrayList<String>();
        public ArrayList<String> FishermanCommand = new ArrayList<String>();
        public ArrayList<String> PilotCommand = new ArrayList<String>();
        public ArrayList<String> MarioCommand = new ArrayList<String>();
        public ArrayList<String> ScoutCommand = new ArrayList<String>();
        public ArrayList<String> ArcherCommand = new ArrayList<String>();
        public ArrayList<String> SquashCommand = new ArrayList<String>();
        public ArrayList<String> SoupCommand = new ArrayList<String>();
        public ArrayList<String> ChestCommand = new ArrayList<String>();
        public ArrayList<String> SquidCommand = new ArrayList<String>();
        public ArrayList<String> BatmanCommand = new ArrayList<String>();
        public ArrayList<String> JuggernautCommand = new ArrayList<String>();
        public ArrayList<String> GravitatorCommand = new ArrayList<String>();
        public ArrayList<String> RemoverCommand = new ArrayList<String>();
        public ArrayList<String> kitused = new ArrayList<String>();
    
        @Override
        public void onEnable() {
            PluginManager pm = getServer().getPluginManager();
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            inst = this;
            menu = new Menu(this);
            if (!setupEconomy() ) {
                getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
    
            Bukkit.getServer().getPluginManager().registerEvents(menu, this);
            pm.registerEvents(new DamageListener(), this);
            pm.registerEvents(new BlockListener(), this);
            pm.registerEvents(new InventoryListener(), this);
            pm.registerEvents(new PlayerListener(this), this);
            pm.registerEvents(new HopperCommand(this), this);
            pm.registerEvents(new JuggernautCommand(this), this);
            pm.registerEvents(new RageCommand(this), this);
            pm.registerEvents(new MarioCommand(this), this);
            pm.registerEvents(new PilotCommand(this), this);
            pm.registerEvents(new SquidCommand(this), this);
            pm.registerEvents(new FishermanCommand(this), this);
            pm.registerEvents(new GravitatorCommand(this), this);
            pm.registerEvents(new RemoverCommand(this), this);
            pm.registerEvents(new SniperCommand(this), this);
            pm.registerEvents(new KillstreakEvent(this), this);
            pm.registerEvents(new Menu(this), this);
            pm.registerEvents(new SquashCommand(this), this);
            getCommand("archer").setExecutor(new ArcherCommand(this));
            getCommand("mario").setExecutor(new MarioCommand(this));
            getCommand("squash").setExecutor(new SquashCommand(this));
            getCommand("hopper").setExecutor(new HopperCommand(this));
            getCommand("pilot").setExecutor(new PilotCommand(this));
            getCommand("rage").setExecutor(new RageCommand(this));
            getCommand("pvp").setExecutor(new KitCommand(this));
            getCommand("scout").setExecutor(new ScoutCommand(this));
            getCommand("sniper").setExecutor(new SniperCommand(this));
            getCommand("squid").setExecutor(new SquidCommand(this));
            getCommand("fisherman").setExecutor(new FishermanCommand(this));
            getCommand("gravitator").setExecutor(new GravitatorCommand(this));
            getCommand("remover").setExecutor(new RemoverCommand(this));
            getCommand("vanish").setExecutor(this);
            getCommand("buyfisherman").setExecutor(this);
            getCommand("buyscout").setExecutor(this);
            getCommand("buypilot").setExecutor(this);
            getCommand("juggernaut").setExecutor(new JuggernautCommand(this));
            getCommand("chest").setExecutor(new ChestCommand(this));
            getCommand("soup").setExecutor(new SoupCommand());
            getCommand("spawn").setExecutor(new SpawnCommand());
    
            System.out.println("KitPVP Enabled!");
        }
    
    
    
        public void onDisabled(){
            System.out.println("KitPVP Disabled!");
        }
        public void events() {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this.Events, this);
        }
    
    
        private boolean setupEconomy() {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = rsp.getProvider();
            return econ != null;
        }
    
    
    
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player player = null;
            if (sender instanceof Player){
                player = (Player) sender;
            }
    
            if (command.getName().equalsIgnoreCase("buyfisherman")){
                if (player != null){
                    EconomyResponse r = econ.withdrawPlayer(player.getName(), 10);
                    if (r.transactionSuccess()) {
                        permission.playerAddTransient(player,"fisherman.set");
                        player.sendMessage(ChatColor.RED + "You bought the Fisherman kit!");
                    }
    
                }
            }else
                if (command.getName().equalsIgnoreCase("buyscout")){
                    if (player != null){
                        EconomyResponse r = econ.withdrawPlayer(player.getName(), 10);
                        if (r.transactionSuccess()) {
                            permission.playerAddTransient(player,"scout.set");
                            player.sendMessage(ChatColor.RED + "You bought the Scout kit!");
                        }
                    }
                }else
                    if (command.getName().equalsIgnoreCase("buypilot")){
                        if (player != null){
                        EconomyResponse r = econ.withdrawPlayer(player.getName(), 10);
                        if (r.transactionSuccess()) {
                            permission.playerAddTransient(player,"pilot.set");
                            player.sendMessage(ChatColor.RED + "You bought the Pilot kit!");
                        }
                    }
                    }
                       
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You cannot vanish!");
                return true;
            }
    
    
            //VanishCommand
            Player p = (Player) sender;
    
            if (command.getName().equalsIgnoreCase("vanish")) {
                if(sender.hasPermission("vanish.set")){
                    if (!vanished.contains(p)) {
                        for (Player pl : Bukkit.getServer().getOnlinePlayers()) {
                            pl.hidePlayer(p);
                        }
                        vanished.add(p);
                        p.sendMessage(ChatColor.GREEN + "You have been vanished!");
                        return true;
                    }
                    else {
                        for (Player pl : Bukkit.getServer().getOnlinePlayers()) {
                            pl.showPlayer(p);
                        }
                        vanished.remove(p);
                        p.sendMessage(ChatColor.GREEN + "You have been unvanished!");
                        return true;
                    }
                }
            }
            return true;
    
        }
    
    
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent event){
            if(event.getEntity() instanceof Player) {
                Player p = (Player) event.getEntity().getPlayer();
                if(event.getEntity().getKiller() instanceof Player) {
                    econ.depositPlayer(p.getKiller(), 80);
                }
    
            }
        }
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event)  {
            Player player = event.getPlayer();
            if (event.getAction() == Action.PHYSICAL) {
                Block block = event.getClickedBlock();
                if (block == null) return;
                if (block.getType() == Material.SOIL){
                    event.setUseInteractedBlock(org.bukkit.event.Event.Result.DENY);
                    event.setCancelled(true);
                    block.setTypeIdAndData(block.getType().getId(), block.getData(), true);
                }
            }
        }
    
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            for (Player p : vanished) {
                if(p.hasPermission("vanish.set")){
                    e.getPlayer().hidePlayer(p);
                }
            }
        }
    
        @EventHandler
        public void onPlayerLeave(PlayerQuitEvent e) {
            vanished.remove(e.getPlayer());
        }
    }
    
    
    http://imgur.com/966a6WK - error
     
  2. Offline

    sirrus86

    Your permissions field is initialized as null and is only set otherwise within the setupPermissions() method. Might be my bad eyesight but I don't see where you use the setupPermissions() method, meaning it isn't being set to anything other than null.
     
  3. Offline

    Treeline1

    The only thing I have with setupPermissions() is this
    Code:
        private boolean setupPermissions() {
            RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
            permission = rsp.getProvider();
            return permission != null;
        }
    
    How do I fix it??
     
  4. Offline

    sirrus86

    Well, since it's a method you created, you need to call it at some point. Logical place would be in the onEnable() method, before the permissions field is needed.
     
  5. Offline

    mine-care

    Why soooo many array lists! :eek:
    and yes i kinda agree with
    @sirrus86 =)
     
  6. Offline

    teej107

  7. Offline

    Treeline1

    Oh my gawd xD Thank you so much! :D
    But now for some reason it still doesnt save the god dam permission!!! Can someone tell me why it isnt saving permission on server reset? =.=

    @mine-care @sirrus86
    Do you think you know why my perms arnt saving on server restart? :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
Thread Status:
Not open for further replies.

Share This Page