NullPointerException

Discussion in 'Plugin Development' started by Kassestral, Jan 31, 2015.

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

    Kassestral

    I am having a problem getting the block below the ItemFrame, can someone please see what I'm doing wrong?

    Code:
    package com.kassestral.plugins.imperium.events;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.block.Sign;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.ItemFrame;
    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.PlayerInteractEntityEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    
    public class ShopHandler implements Listener {
       
        @EventHandler
        public void createShop(PlayerInteractEntityEvent event){
            Player player = event.getPlayer();
            Entity entityClicked = event.getRightClicked();
            
            if(entityClicked instanceof ItemFrame){
                ItemFrame itemframe = (ItemFrame) entityClicked;
               
                if(player.isOp()) {
                    itemframe.setItem(player.getItemInHand());
                    setSign(itemframe.getLocation(), itemframe.getItem().getType().toString());
                }
            }
        }
       
        @SuppressWarnings("deprecation")
        @EventHandler
        public void openShop(PlayerInteractEvent event){
            Player player = event.getPlayer();
           
            if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                Sign sign = (Sign) event.getClickedBlock();
               
                if(getSignType(sign.getLocation()) != "none") {
                    Inventory inventory = Bukkit.createInventory(null, 9, ChatColor.WHITE + "Shop");
                    ItemStack placefiller = new ItemStack(160, 1);
                    ItemStack buying = getItemFrameItem(sign.getLocation());
                    ItemStack selling = getItemFrameItem(sign.getLocation());
                   
                    placefiller.getItemMeta().setDisplayName("");
                    buying.getItemMeta().setDisplayName(ChatColor.WHITE + "Buying");
                    selling.getItemMeta().setDisplayName(ChatColor.WHITE + "Selling");
                   
                    inventory.setItem(0, placefiller);
                    inventory.setItem(1, placefiller);
                    inventory.setItem(2, placefiller);
                    inventory.setItem(3, buying);
                    inventory.setItem(4, placefiller);
                    inventory.setItem(5, selling);
                    inventory.setItem(6, placefiller);
                    inventory.setItem(7, placefiller);
                    inventory.setItem(8, placefiller);
                   
                    player.openInventory(inventory);
                }
            }
        }
       
        private String getSignType(Location location) {
           
            if(location.getBlock().getState() instanceof Sign) {
                Sign shop_sign = (Sign) location.getBlock().getState();
               
                if(shop_sign.getLine(1).equalsIgnoreCase(ChatColor.WHITE + "Buy")) {
                    return "buy";
                }
                else if(shop_sign.getLine(1).equalsIgnoreCase(ChatColor.WHITE + "Sell")) {
                    return "sell";
                }
                else {
                    return "none";
                }
            }
            else {
                return "none";
            }
        }
       
        private ItemStack getItemFrameItem(Location location) {
            Location itemframe_location = location;
            itemframe_location.setY(location.getY() + 1);
           
            ItemFrame itemframe = (ItemFrame) Bukkit.getWorld(location.getWorld().toString()).getBlockAt(itemframe_location);
            return itemframe.getItem();
        }
       
        private void setSign(Location location, String ID) {
            Location sign_location = location;
            sign_location.setY(location.getY() - 1);
           
            Block block = Bukkit.getServer().getWorld(sign_location.getWorld().toString()).getBlockAt(sign_location);
           
            block.setType(Material.SIGN);
           
            Sign sign = (Sign) block.getState();
            sign.setLine(0, "=======");
            sign.setLine(1, ChatColor.WHITE + "Shop");
            sign.setLine(2, ChatColor.WHITE + ID);
            sign.setLine(3, "=======");
        }
    }
    Error:
    Code:
    Caused by: java.lang.NullPointerException
            at com.kassestral.plugins.imperium.events.ShopHandler.setSign(ShopHandle
    r.java:102) ~[?:?]
            at com.kassestral.plugins.imperium.events.ShopHandler.createShop(ShopHan
    dler.java:32) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _25]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _25]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_25]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_25]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:292) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            ... 13 more
     
  2. Offline

    Skionz

    @Kassestral Your using toString on an object and trying to get a world from it. This is completely redundant. You don't need to invoke Bukkit.getWorld when you already have the world stored in your Location object.
     
  3. Offline

    Kassestral

    @Skionz
    In all fairness it's 5:40am and I can't think straight xD completely forgot all about that, I'm now getting this error though
    Code:
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R3.block.Cr
    aftBlockState cannot be cast to org.bukkit.block.Sign
            at com.kassestral.plugins.imperium.events.ShopHandler.setSign(ShopHandle
    r.java:106) ~[?:?]
            at com.kassestral.plugins.imperium.events.ShopHandler.createShop(ShopHan
    dler.java:32) ~[?:?]
     
  4. Offline

    Skionz

    @Kassestral The block's state isn't an instance of Sign.
     
  5. Offline

    Kassestral

    @Skionz
    How can I get the ItemFrame from a location?
    I managed to fix the previous error about sign blockstate
     
  6. Offline

    caderape

    Block block = Bukkit.getServer().getWorld(sign_location.getWorld().toString()).getBlockAt(sign_location);

    why not a simple sign_location.getBlock() ?

    if (sign_location.getBlock().getState() instanceof Sign (import the Material.Block){
    Sign sign = sign_location.getBlock().getState();

    This should totally work if the block casted is a sign.@Kassestral
     
  7. Offline

    Kassestral

    @caderape
    Thanks for the help, but if you read my previous comment then you will know I fixed that error, but have a different one
     
  8. Offline

    Skionz

    @Kassestral He just told you how to fix that. What is the new issue?
     
  9. Offline

    Kassestral

    @Skionz
    How can I get the ItemFrame from a location? since the ItemFrame is an instance of an Entity
     
  10. Offline

    caderape

    Location loc = your itemframe
    for (Entity ent : loc.getChunk().getEntities()){
    if (ent.getLocation().equals(loc) && ent instanceof itemframe) {
    //do stuff
    }
    }

    May be something liek this ?
    @Kassestral
     
    Last edited: Jan 31, 2015
  11. Offline

    sirrus86

    Best approach here would be to get the entities in the location's chunk and determine which is closest to the specified location. Keep in mind though you can theoretically have four different ItemFrames in a given block location.
     
Thread Status:
Not open for further replies.

Share This Page