ItemStack[] - The local variable may not have been initialised yet

Discussion in 'Plugin Development' started by Crimsonfox, Mar 16, 2011.

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

    Crimsonfox

    Right, excuse this for probably being very simple but I am new the scene and I have exhausted Google, forum search, etc etc

    I'm getting the problem where I declare ItemStack[] chest_NOAIR; I get compile errors saying the variable may have not been initialised yet. I've tried relocating it and tried setting it to null but then I get a nullpointerException when I click a chest in-game.

    Hope that's enough info, hopefully thanks in advance. =)

    Code:
    public void onBlockInteract(BlockInteractEvent event){
        System.out.println(event.getBlock().getType());
    //    Material chest;
    
        if (event.getBlock().getTypeId() == 54){
            System.out.println("It's a chest!");
            Chest test = (Chest)event.getBlock().getState();
            CraftChest blockTest = (CraftChest)event.getBlock().getState();
            CraftInventory test2 = (CraftInventory)blockTest.getInventory();
        //    System.out.println(test.getInventory());
    
            ItemStack[] chest_contents = blockTest.getInventory().getContents();
            System.out.println(chest_contents[0] + "Item at 0");
            System.out.println(chest_contents.length);
    
            int newItemsStackCount = 0;
            int chest_length = chest_contents.length;
            ItemStack[] chest_NOAIR;
            for (int i = 0; i < chest_length; i++){
    
                if (chest_contents[i].getTypeId() != 0) {
                    chest_NOAIR[newItemsStackCount] = chest_contents[i];
                    newItemsStackCount++;
                }
                }
            System.out.println(chest_NOAIR.length);
            }
    }
     
  2. Offline

    Edward Hand

    That's because you haven't initialised it [​IMG]

    Do something like this:
    Code:
    ItemStack[] chest_NOAIR = new ItemStack[lengthOfArray];
    (replacing lengthOfArray with an appropriate number)
     
  3. Offline

    Crimsonfox

    Thanks for the quick reply!

    Does that mean you have to know the length the array will end up at? Eg. If I set that lengthOfArray to 30, I'm going to be outputting a length of 30 every time. I presume that simply using a number above that initialised number doesn't create a new entry in the array?

    Edit: Unless I then have to search the array until it becomes a null value? Seems like one too many searches?

    Edit2: So I currently have this after it gets all the stuff that's not AIR:
    Code:
    for (int i = 0; i < chest_NOAIR.length; i++) {
                if (chest_NOAIR[i] != null) System.out.println(chest_NOAIR[i]);
    Which as I said works, but is another search...

    Edit3: Duh, already found how many spaces in the array get filled up. Replace that < chest_NOAIR.length with the counter I used earlier to put them in the correct cells.

    I really should take 5 minutes before I re-edit my post. :p Though I do find typing something out tends to make me think of a way to fix it. ^^
     
  4. Offline

    Edward Hand

    Another solution is to use ArrayLists. They are more dynamic than arrays. They have no defined length. Their length grows and shrinks as your add and remove things from them.
     
Thread Status:
Not open for further replies.

Share This Page