Trouble working with ArrayList

Hey guys, I’m currently working on adding items to an inventory (which is an arraylist) and making them display when I go to bring up my inventory, heres the code that I have for it so far.

This is in my “InventoryState” class


for(ItemEntity p: Player.playerInventory){
	  if(Player.playerInventory.get(0) != null)                //my problem is in this for statement somehow
	  g.drawImage(p.getSprite(),50,50,this);
	  if(Player.playerInventory.get(1) != null)               //if i take out this "if statment" than I'm able to display the 1st item fine.
		  g.drawImage(p.getSprite(),100,100,this);}

This is in my “Player” class


public static ArrayList<ItemEntity> playerInventory = new ArrayList <ItemEntity>(16);

if(GameLoop.keysPressed[69]==true){     // picks up an item and adds it to the player inventory
		  for(Entity p : GamePlayState.entities){
				 if (p instanceof ItemEntity){
					 if(xCord-p.getX()<15&&yCord-p.getY()<15&&xCord-p.getX()>-15&&yCord-p.getY()>-15){
						 playerInventory.add((ItemEntity) p);
						 p.Hit(p,Entity.HitEnum.pickup,0);}}}}
    if(GameLoop.keysDown[73] == true)//opens inventory state
    	  StateStack.pushState("InventoryState");

Ok so, if I go in my game and pick up 2 or more items with the code I have above and then switch to my inventory state than everything will display as expected, but if I were to pick up just 1 item and switch to my inventory than I get an ArrayOutOfBounds Exception.
If I were to remove the 2nd “if statement” so it would just be.


for(ItemEntity p: Player.playerInventory){
	  if(Player.playerInventory.get(0) != null)         
	  g.drawImage(p.getSprite(),50,50,this);}

Than the 1 item displays properly in the inventory without an exception. I really feel like its somthing very simple thats going on here, but after messing with it for awhile I cant seem to pinpoint how to work with this. Any help would be greatly appreciated (:

If the list is 1 item long then get(1) will give an out of bounds exception, not null.

You want to do a loop like this, it will work for any size of list:

for (InventoryItem item : player.playerInventory) { ... }

Your variables look wrong - I assume Player is an instance not a class, so it should be named “player”.

As sirkarfpen says below, why do you loop over the items twice? I misread your code, I thought the outer loop was iterating over the players, and the inner loop over the inventories, which would make a bit more sense.

Why would you check the playerInventory with get(), when your allready getting each ItemEntity with:
for(ItemEntity p : Player.playerInventory)
That would be my solution to looping through your inventory:


int pX = 50;
int pY = 50;
for(ItemEntity p : Player.playerInventory()) {
    if(p != null) {
        g.drawImage(p.getSprite(),pX, pY, this);
        pX+=50;
        pY+=50;
    }
}

Or maybe Player is a class and he is accessing it in a static way^^.

A static inventory on a Player - that makes no sense to me. I suppose it might be possible in a single player game where the player code is totally different to every other entity’s code…

Ah, I didnt mean for the inventory to be static, lol that wouldv definitely given me some problems later on, but anyways now I’m not sure how I’m going to render the sprites in playerInventory, looks like i’ll need to figure out that first x.x, thanks for pointing that out to me though!

Just loop over the inventory, and render anything that’s not null. See sirkarfpen’s snippet.

Even better, write the code so you don’t have any nulls in your inventory. Having nulls in the inventory could lead to conversations like this:

Bilbo: “what have I got in my pocket?”

Gollum: “I give up.”

Bilbo: “A handkerchief, a nothing, a ring, two more nothings, and a halfpenny.”

yeah, I suppose I’m going to make a method in my player instance that handles the inventory so that I can use that code for it, thanks so much guys (: