Null Pointer When Calling Variable

Discussion in 'Plugin Development' started by Airbornz, Dec 18, 2014.

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

    Airbornz

    Hello,

    In this compass plugin when calling a player from a class variable (Which I declare in another method) im getting a null error. Errors are coming from line 43, and line 31.

    Code:
    package me.Airbornz.CompassWarps;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    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.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class CompassListener implements Listener{
    
        public static Player eplayer;
      
        @EventHandler
        public void onCompass(PlayerInteractEvent event){
            Action eaction = event.getAction();
            Player player = event.getPlayer();
            ItemStack hand = player.getItemInHand();
            ItemMeta meta = hand.getItemMeta();
            CompassListener.eplayer = player;
            if (eaction.equals(Action.RIGHT_CLICK_AIR) || eaction.equals(Action.RIGHT_CLICK_BLOCK)){
                if (hand != null && hand.getType() == Material.COMPASS){
                    if (meta.getDisplayName()=="Warp Selector"){
                        CompassInv();
                        player.openInventory(CompassInv());
                    }
                }
            }
        }
      
        public static Inventory CompassInv(){
          
            Inventory inv = Bukkit.createInventory(eplayer, 18, "Select A Mine...");
            ItemStack item = null;
            ItemMeta meta = null;
            List<String> lore = new ArrayList <String>();
            if (eplayer.hasPermission("compass.A")){
                item = new ItemStack(Material.EMERALD_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§aWarp To A Mine");
                lore.add("Teleport To A Mine!");
            }
            else{
                item = new ItemStack(Material.REDSTONE_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§8Warp To A Mine (LOCKED)");
                lore.add("Teleport To A Mine!");
            }
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.addItem(item);
            if (eplayer.hasPermission("compass.B")){
                item = new ItemStack(Material.EMERALD_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§aWarp To B Mine");
                lore.add("Teleport To B Mine!");
            }
            else{
                item = new ItemStack(Material.REDSTONE_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§8Warp To B Mine (LOCKED)");
                lore.add("Teleport To B Mine!");
            }
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.addItem(item);
            if (eplayer.hasPermission("compass.C")){
                item = new ItemStack(Material.EMERALD_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§aWarp To C Mine");
                lore.add("Teleport To B Mine!");
            }
            else{
                item = new ItemStack(Material.REDSTONE_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§8Warp To C Mine (LOCKED)");
                lore.add("Teleport To C Mine!");
            }
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.addItem(item);
            if (eplayer.hasPermission("compass.D")){
                item = new ItemStack(Material.EMERALD_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§aWarp To D Mine");
                lore.add("Teleport To D Mine!");
            }
            else{
                item = new ItemStack(Material.REDSTONE_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§8Warp To D Mine (LOCKED)");
                lore.add("Teleport To D Mine!");
            }
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.addItem(item);
            if (eplayer.hasPermission("compass.E")){
                item = new ItemStack(Material.EMERALD_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§aWarp To E Mine");
                lore.add("Teleport To E Mine!");
            }
            else{
                item = new ItemStack(Material.REDSTONE_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§8Warp To E Mine (LOCKED)");
                lore.add("Teleport To E Mine!");
            }
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.addItem(item);
            if (eplayer.hasPermission("compass.F")){
                item = new ItemStack(Material.EMERALD_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§aWarp To F Mine");
                lore.add("Teleport To D Mine!");
            }
            else{
                item = new ItemStack(Material.REDSTONE_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§8Warp To F Mine (LOCKED)");
                lore.add("Teleport To F Mine!");
            }
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.addItem(item);
            if (eplayer.hasPermission("compass.G")){
                item = new ItemStack(Material.EMERALD_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§aWarp To G Mine");
                lore.add("Teleport To G Mine!");
            }
            else{
                item = new ItemStack(Material.REDSTONE_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§8Warp To G Mine (LOCKED)");
                lore.add("Teleport To G Mine!");
            }
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.addItem(item);
            if (eplayer.hasPermission("compass.H")){
                item = new ItemStack(Material.EMERALD_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§aWarp To H Mine");
                lore.add("Teleport To H Mine!");
            }
            else{
                item = new ItemStack(Material.REDSTONE_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§8Warp To H Mine (LOCKED)");
                lore.add("Teleport To H Mine!");
            }
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.addItem(item);
            if (eplayer.hasPermission("compass.I")){
                item = new ItemStack(Material.EMERALD_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§aWarp To I Mine");
                lore.add("Teleport To I Mine!");
            }
            else{
                item = new ItemStack(Material.REDSTONE_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§8Warp To I Mine (LOCKED)");
                lore.add("Teleport To I Mine!");
            }
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.addItem(item);
            if (eplayer.hasPermission("compass.J")){
                item = new ItemStack(Material.EMERALD_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§aWarp To J Mine");
                lore.add("Teleport To J Mine!");
            }
            else{
                item = new ItemStack(Material.REDSTONE_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§8Warp To J Mine (LOCKED)");
                lore.add("Teleport To J Mine!");
            }
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.addItem(item);
            if (eplayer.hasPermission("compass.K")){
                item = new ItemStack(Material.EMERALD_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§aWarp To K Mine");
                lore.add("Teleport To K Mine!");
            }
            else{
                item = new ItemStack(Material.REDSTONE_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§8Warp To K Mine (LOCKED)");
                lore.add("Teleport To K Mine!");
            }
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.addItem(item);
            if (eplayer.hasPermission("compass.L")){
                item = new ItemStack(Material.EMERALD_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§aWarp To L Mine");
                lore.add("Teleport To L Mine!");
            }
            else{
                item = new ItemStack(Material.REDSTONE_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§8Warp To L Mine (LOCKED)");
                lore.add("Teleport To L Mine!");
            }
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.addItem(item);
            if (eplayer.hasPermission("compass.PVP")){
                item = new ItemStack(Material.EMERALD_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§aWarp To PVP Mine");
                lore.add("Teleport To PVP Mine!");
            }
            else{
                item = new ItemStack(Material.REDSTONE_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§8Warp To PVP Mine (LOCKED)");
                lore.add("Teleport To PVP Mine!");
            }
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.addItem(item);
            if (eplayer.hasPermission("compass.Staff")){
                item = new ItemStack(Material.EMERALD_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§aWarp To Staff Mine");
                lore.add("Teleport To H Mine!");
            }
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.addItem(item);
            if (eplayer.hasPermission("compass.VIP")){
                item = new ItemStack(Material.EMERALD_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§aWarp To VIP Mine");
                lore.add("Teleport To VIP Mine!");
            }
            else{
                item = new ItemStack(Material.REDSTONE_BLOCK);
                meta = item.getItemMeta();
                meta.setDisplayName("§8Warp To VIP Mine (LOCKED)");
                lore.add("Teleport To VIP Mine!");
            }
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.addItem(item);
          
            return inv;
        }
    }
    

    Thanks in advance,
    Airbornz
     
  2. Offline

    Skionz

    @Airbornz There is no need for static, nor 'eplayer.' Pass your player instance through your methods parameters. Also you should follow good naming conventions; your code is hard to read.
     
  3. Offline

    Airbornz

    How would one go about doing that (Passing the instance), having trouble myself.
     
  4. Offline

    mythbusterma

    @Airbornz

    First of all, take the static off that method, then add an argument to that function, then change the case to match Java naming conventions.
     
  5. Offline

    Skionz

    @Airbornz By using parameters in your method :oops:
     
  6. Offline

    Airbornz

    I would need the static as Im calling it in other classes.

    Yes, Ive done that except im still getting a null error :/

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

    nverdier

    No they are saying instead of using static, pass an instance to the other classes you need it in.
     
  8. Offline

    mythbusterma

    @Airbornz

    Well I told you how to fix your problem as well, but no you don't, there's this thing called an accessor method, it's kinda the bread and butter of OOP.
     
    teej107 likes this.
  9. Offline

    nverdier

    Well said.
     
  10. Offline

    teej107

  11. Offline

    Airbornz

    While I would love to remove such static, eclipse keeps telling me that I cannot call a non-static method :/.

    While I do know how to use accessor methods in a sense, it isn't working in my case.

    @mythbusterma @Skionz

    In this code I'm adding the instances, yet it will still point to null :/

    Code:
            return player;
        }
        public static Inventory CompassInv(Player player){
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  12. Offline

    mythbusterma

    @Airbornz

    Where are you calling that method? And yes you can, you just have to have an instance of the other class to use it.
     
  13. Offline

    Skionz

    Blame it on eclipse... You can remove the static, you just have to know how, and if you don't you should refer to this, this, and this
     
  14. Offline

    teej107

    Don't depend on your IDE like that. Remove all static keywords from your code and that is the first step. You should never listen to what machines tell you to do. They'll eventually start taking over the world if you do that!
     
    Skionz likes this.
  15. Offline

    Airbornz

    Im calling it in onCompass

    Code:
                    if (meta.getDisplayName()=="Warp Selector"){
                        CompassInv(player);
                        player.openInventory(CompassInv(player));
                    }
                }
            }
            return player;
        }
        public static Inventory CompassInv(Player player){
     
  16. Offline

    Skionz

  17. Offline

    mythbusterma

    @Airbornz

    So do you still have the issue, and please, remove static from your "CompassInv" method and fix the capitalisation.
     
  18. Offline

    Airbornz

    Yes im still having such issue, and I don't think capitalization matters, does it?

    But if I change it to non-static I will get this error:
    --------------------------------------------------------------------
    Cannot make a static reference to the non-static method compassinv(Player) from the type CompassListener CompassListener.java /CompassWarps/src/me/Airbornz/CompassWarps line 28 Java Problem
    ---------------------------------------------------------------------

    Is that ok, or will I have to change some other things?

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

    teej107

    @Airbornz Yes that is ok but you still have to make more changes obviously.
     
  20. Offline

    Airbornz

  21. Offline

    Skionz

    @Airbornz
     
  22. Offline

    teej107

Thread Status:
Not open for further replies.

Share This Page