why is this giving me Null exemptons??

  • java.lang.NullPointerException
    at BootyBay.PlayerInventoryToChestEvent(BootyBay.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at pluginapi.PluginEventHandler.triggerEvent(SourceFile:201)
    at aU.f.a(SourceFile:883)
    at aU.f.messageReceived(SourceFile:119)
    at k.e.a(SourceFile:109)
    at k.c.run(SourceFile:65)


    why does reurn a NULL exeption?

  • the problem is with
    line 8 on


    this is the Whole Block, the cose itself is 300 lines long.

  • ok the null pointer exception probably happens because this line: Item item = chest.getItem(1); returns a null item and then the item.getName().equals("bandage") compares the name of a null value i.e. a null again with the word bandage

  • ok the null pointer exception probably happens because this line: Item item = chest.getItem(1); returns a null item and then the item.getName().equals("bandage") compares the name of a null value i.e. a null again with the word bandage

    Yes. When comparing something in Java, you are better to compare a constant with a variable, or check if the variable has a value first. One of :


    if (item.getName() != null && item.getName().equals("bandage")){


    or better


    if ("bandage".equals(item.getName()){


    But in your case, since getName() may be the cause of your NPE, you must check for null, so


    if (item != null && "bandage".equals(item.getName()){

  • Java
    @EventMethod
    public void PlayerInventoryToChestEvent(PlayerInventoryToChestEvent event){
    Player player = event.getPlayer();
    final int infoId = event.getChestID();
    String Pname = player.getName();
    Chest chest = getWorld().getChest(infoId);
    Item[] Items = chest.getItems();
    // player.sendTextMessage("yes"+NameofItem);
    if (items.getName() != null && item.getName().equals("bandage")){
    }

    ive Tried all Three Methods and get "Cannot Find Symbol"


    if i use getitem without (1) i get "Found no Arguments"



    this CANNOT be done
    Maybe Red can explain why we need an argument for getItem();

  • What i Evetually want this to do is check Weather an item is "Sellable" and the Sell Price (the Prices will change and be Written to a databse )
    right now this is just to get the man code working,
    Im using a chest to "eat"the item and give the correct amout of money for it.

  • Item item = chest.getItem(1); this is the Correct Syntax.

    yes this is the correct syntax but that 1 is the slot number in the chest so to access all items in a chest one by one you should use a for loop around that item line.


    like this:


    Java
    int SlotNum = chest.getSlotCount()
    for (int i=0;i<SlotNum;i++){
    Item item = chest.getItem(i);
    //do whatever you want to the current item here
    }
  • If you read an item from a specific chest slot, always make sure the "Item" object isn't null. For example, when reading the item from the first chest slot (Item item = chest.getItem(0);), it returns null if there is no item in this slot.


    Maybe Red can explain why we need an argument for getItem();


    [...]


    It Has to be the limitatons of the API, I know what you are getting at at and
    Item item = chest.getItem(); cannot be done.

    This is not a limitation of the API. There is no chest.getItem() function, since it would not make sense. What do you expect such a function to return? A chest has usually more than 1 slot, so it is necessary to specify which item you want to get (from which slot). That's why you have to use the chest.getItem(int slot) function.
    If you want to get all Items from a chest instead, you can use the chest.getItems() function, although keep in mind that this returns an array of items.


    Code
    Item[] Items = chest.getItems();
    //Item item = event.getItem();
    // player.sendTextMessage("yes"+NameofItem);
    if (Items.getName() != null && Items.getName().equals("bandage")){
    player.sendTextMessage("[#FF0000]BootyBux: ");
    }

    This does not work: The chest.getItems() function returns an array of all items which are in the chest. You can't call getName() on an array. Arrays in Java do not have any methods. Instead, you need to get a certain item from the array (in this case Item item = Items[0];), or iterate through the array:

    Java
    Item[] items = chest.getItems();
    for(Item item : items){
    if(item != null && item.getName().equals("bandage")){
    player.sendTextMessage("Found bandage!");
    break;
    }
    }

Participate now!

Don’t have an account yet? Create a new account now and be part of our community!