Jump to content

Recommended Posts

Posted

this code isn't working (as in it crashes with NullPointerException):

 

@Override
public void updateTick(World world,int x,int y,int z,Random rand){
	CrucibleTileEntity te = (CrucibleTileEntity)world.getTileEntity(x, y, z);

	List<EntityItem> items = world.getEntitiesWithinAABB(EntityItem.class, AxisAlignedBB.getBoundingBox(x+0D, y+1D, z+0D, x+1D, y+2D, z+1D));

	ItemStack item=null;

	if(items.size()>0){


		item = items.get(items.size()-1).getEntityItem();
		if(CrucibleRecipeHandler.alloweds.contains(item.getItem())){

			int Counter=0;

			for(ItemStack i : inventory){
				Counter++;
				if(i.isItemEqual(item))
					if(i.isItemEqual(inventory.get(Counter-1))){
						inventory.add(new ItemStack(i.getItem(),i.stackSize+inventory.get(Counter-1).stackSize));
						inventory.remove(Counter-1);
					break;
					}
			}
			Counter=0;


			inventory.add(item);
			items.get(items.size()-1).setDead();
			item = null;
		}

 

inventory is an ArrayList stored outside the function. Yes I  realize I have to use a tileentity etc, this is just for testing purposes.

The proud(ish) developer of Ancients

Posted

I changed it to:

item = items.get(items.size()-1).getEntityItem();
		if(CrucibleRecipeHandler.alloweds.contains(item.getItem())){

			int Counter=0;

			for(ItemStack i : inventory){
				Counter++;
				if(i.isItemEqual(item))
					if(inventory.get(Counter)!=null)
					if(i.isItemEqual(inventory.get(Counter-1))){
						inventory.add(new ItemStack(i.getItem(),i.stackSize+inventory.get(Counter-1).stackSize));
					break;
					}
			}
			if(inventory.get(Counter-1)!=null)
			inventory.remove(Counter-1);
			Counter=0;


			inventory.add(item);
			items.get(items.size()-1).setDead();
			item = null;
		}

it still doesn't work, keep getting crashes on my null check... "if(inventory.get(Counter-1)!=null)"

The proud(ish) developer of Ancients

Posted

Is your inventory ArrayList ever empty for any reason?

 

If so, then it will crash on that line since you're checking the index position of Counter-1, which will be -1 if the counter is not incremented. Now, the counter is only incremented within your augmented for-loop, which won't execute if there are no elements in the inventory ArrayList.

Posted

I'd say you could do a if(inventory != null) check before that get() call.

 

I also see that you do not have brackets for the if-statements, which only works for one-line statements after the if()

--Remember to "Thank you" posts that actually helped you, and if a person seems nice, why not give them an applaud while you're at it--

Posted

first, wouldn't the ArrayList = a new ArrayList?  Wouldn't it not be null then? or is that how "new" works.

 

also They're nested if statements, I didn't need the brackets.

The proud(ish) developer of Ancients

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.