Jump to content

[1.8] Tile Entity NPE


The_SlayerMC

Recommended Posts

While trying to make some kind of "crafting table" by checking the items in the slots I seem to get a crash when i try to remove an item from any slot in the Tile Entity (Inventory slots work fine)

 

Here is what the GUI looks like:

http://prntscr.com/6rh5df

 

I get a NPE at the line

for(int i = 0; i < inventory.length; i++)

In the Tile entity -> public void writeToNBT(NBTTagCompound nbt)

 

[spoiler=Container]

public class ContainerSummoningTable extends Container {

public IInventory tableInventory;
private World world;

public ContainerSummoningTable(InventoryPlayer inventory, TileEntitySummoningTable entity, World w) {
	world = w;
	tableInventory = entity;
	this.addSlotToContainer(new Slot(entity, 0, 44, 17));
	this.addSlotToContainer(new Slot(entity, 1, 44, 35));
	this.addSlotToContainer(new Slot(entity, 2, 44, 53));
	this.addSlotToContainer(new Slot(entity, 3, 80, 35));
	this.addSlotToContainer(new Slot(entity, 4, 117, 17));
	this.addSlotToContainer(new Slot(entity, 5, 117, 35));
	this.addSlotToContainer(new Slot(entity, 6, 117, 53));

	for(int i = 0; i < 3; ++i) for(int j = 0; j < 9; ++j) this.addSlotToContainer(new Slot(inventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
	for(int i = 0; i < 9; ++i) this.addSlotToContainer(new Slot(inventory, i, 8 + i * 18, 142));
}

@Override
public void onContainerClosed(EntityPlayer playerIn) {
	super.onContainerClosed(playerIn);
	/*if(!world.isRemote) {
		ItemStack itemstack = this.tableInventory.getStackInSlotOnClosing(0);
		if (itemstack != null) {
			playerIn.dropPlayerItemWithRandomChoice(itemstack, false);
		}
	}*/
}

@Override
public boolean canInteractWith(EntityPlayer playerIn) {
	return true;
}
}

 

 

 

[spoiler=Tile Entity]

public class TileEntitySummoningTable extends TileEntity implements IUpdatePlayerListBox, IInventory {

private ItemStack[] inventory;

public TileEntitySummoningTable() {
	this.inventory = new ItemStack[7];
}

@Override
public void writeToNBT(NBTTagCompound nbt) {
	super.writeToNBT(nbt);
	NBTTagList nbttaglist = new NBTTagList();
	for(int i = 0; i < inventory.length; i++) {
		if(this.inventory[i] != null) {
			NBTTagCompound nbttagcompound1 = new NBTTagCompound();
			nbttagcompound1.setByte("Slot", (byte)i);
			this.inventory[i].writeToNBT(nbttagcompound1);
			nbttaglist.appendTag(nbttagcompound1);
		}
	}
	nbt.setTag("Items", nbttaglist);
}

@Override
public void readFromNBT(NBTTagCompound nbt) {
	super.readFromNBT(nbt);
	NBTTagList nbttaglist = nbt.getTagList("Items", 10);
	for(int i = 0; i < nbttaglist.tagCount(); i++) {
		NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
		byte b0 = nbttagcompound1.getByte("Slot");
		if(b0 >= 0 && b0 < this.inventory.length)
			this.inventory[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
	} 
}

@Override
public void update() {
	/*if(inventory[0] != null && inventory[1] != null && inventory[2] != null && inventory[3] != null && inventory[4] != null && inventory[5] != null && inventory[6] != null) {
		Item item1 = inventory[0].getItem(), item2 = inventory[1].getItem(), item3 = inventory[2].getItem(), item4 = inventory[3].getItem(), item5 = inventory[4].getItem(), item6 = inventory[5].getItem(), item7 = inventory[6].getItem();
		if(item1 == EssenceItems.boilPowder && item2 == EssenceItems.boilPowder && item3 == EssenceItems.boilPowder && item4 == EssenceItems.blazingFireball && item5 == EssenceItems.boilPowder && item6 == EssenceItems.boilPowder && item7 == EssenceItems.boilPowder) {
			inventory[3].setItem(EssenceItems.blazierOrb);
		}
	}*/
}

@Override
public int getSizeInventory() {
	return inventory.length;
}

@Override
public ItemStack getStackInSlot(int i) {
	return inventory[i];
}

@Override
public String getName() {
	return "Summoning Table";
}

@Override
public boolean hasCustomName() {
	return true;
}

@Override
public IChatComponent getDisplayName() {
	return null;
}

@Override
public ItemStack decrStackSize(int i, int j) {
	if(inventory[i] != null) {
		if(inventory[i].stackSize <= j) {
			ItemStack itemstack = inventory[i];
			inventory = null;
			return itemstack;
		} else {
			inventory[i].stackSize -= j;
			return new ItemStack(inventory[i].getItem(), j, inventory[i].getMetadata());
		}
	} else {
		return null;
	}
}

@Override
public ItemStack getStackInSlotOnClosing(int index) {
	if(this.inventory[index] != null)  {
		ItemStack itemstack = inventory[index];
		this.inventory = null;
		return itemstack;
	} else {
		return null;
	}
}

@Override
public void setInventorySlotContents(int index, ItemStack stack) {
	boolean flag = stack != null && stack.isItemEqual(this.inventory[index]) && ItemStack.areItemStackTagsEqual(stack, this.inventory[index]);
	this.inventory[index] = stack;
	if(stack != null && stack.stackSize > this.getInventoryStackLimit())
		stack.stackSize = this.getInventoryStackLimit();
	if(index == 0 && !flag) this.markDirty();
}

@Override
public int getInventoryStackLimit() {
	return 64;
}

@Override
public boolean isUseableByPlayer(EntityPlayer player) {
	return true;
}

@Override
public void openInventory(EntityPlayer player) { }

@Override
public void closeInventory(EntityPlayer player) { }

@Override
public boolean isItemValidForSlot(int index, ItemStack stack) {
	return stack.getItem() != null;
}

@Override
public int getField(int id) {
	return 0;
}

@Override
public void setField(int id, int value) { }

@Override
public int getFieldCount() {
	return 0;
}

@Override
public void clear() {
	for(int i = 0; i < getSizeInventory(); i++) inventory[i] = null;
}
}

 

 

 

[spoiler=Crash]

[15:24:40] [server thread/ERROR] [FML]: A TileEntity type net.essence.blocks.tileentity.TileEntitySummoningTable has throw an exception trying to write state. It will not persist. Report this to the mod author
java.lang.NullPointerException
at net.essence.blocks.tileentity.TileEntitySummoningTable.writeToNBT(TileEntitySummoningTable.java:26) ~[TileEntitySummoningTable.class:?]
at net.minecraft.world.chunk.storage.AnvilChunkLoader.writeChunkToNBT(AnvilChunkLoader.java:382) [AnvilChunkLoader.class:?]
at net.minecraft.world.chunk.storage.AnvilChunkLoader.saveChunk(AnvilChunkLoader.java:183) [AnvilChunkLoader.class:?]
at net.minecraft.world.gen.ChunkProviderServer.saveChunkData(ChunkProviderServer.java:246) [ChunkProviderServer.class:?]
at net.minecraft.world.gen.ChunkProviderServer.saveChunks(ChunkProviderServer.java:305) [ChunkProviderServer.class:?]
at net.minecraft.world.WorldServer.saveAllChunks(WorldServer.java:938) [WorldServer.class:?]
at net.minecraft.server.MinecraftServer.saveAllWorlds(MinecraftServer.java:363) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.stopServer(MinecraftServer.java:395) [MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.stopServer(IntegratedServer.java:328) [integratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:531) [MinecraftServer.class:?]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_20]

 

 

Former developer for DivineRPG, Pixelmon and now the maker of Essence of the Gods

Link to comment
Share on other sites

You're putting null to the field 'inventory' when getting items from a slot. It should be inventory.

@Override
public ItemStack decrStackSize(int i, int j) {
	if(inventory[i] != null) {
		if(inventory[i].stackSize <= j) {
			ItemStack itemstack = inventory[i];
			inventory = null;
			return itemstack;
		} else {
			inventory[i].stackSize -= j;
			return new ItemStack(inventory[i].getItem(), j, inventory[i].getMetadata());
		}
	} else {
		return null;
	}
}

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Link to comment
Share on other sites

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.