Jump to content

[SOLVED] NullPointerException when adding Slots in the Container


Emonadeo

Recommended Posts

So basically every time when I try to open the GUI of my block called "Storage Assembler" I am receiving a NullPointerException which refers to this line in the Container class:

this.addSlotToContainer(new Slot(tile.inventory, 1, 44, 21));

 

I have made a working gui before in another mod and I compared the two source codes but I cant find the mistake. (Probably an obvious one) :D

 

Full Crash Log:

   

[22:20:29] [server thread/FATAL]: Error executing task
    java.util.concurrent.ExecutionException: java.lang.NullPointerException
    	at java.util.concurrent.FutureTask.report(Unknown Source) ~[?:1.8.0_77]
    	at java.util.concurrent.FutureTask.get(Unknown Source) ~[?:1.8.0_77]
    	at net.minecraft.util.Util.runTask(Util.java:26) [util.class:?]
    	at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:742) [MinecraftServer.class:?]
    	at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:687) [MinecraftServer.class:?]
    	at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:156) [integratedServer.class:?]
    	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:536) [MinecraftServer.class:?]
    	at java.lang.Thread.run(Unknown Source) [?:1.8.0_77]
    Caused by: java.lang.NullPointerException
    	at container.ContainerStorageAssembler.<init>(ContainerStorageAssembler.java:17) ~[ContainerStorageAssembler.class:?]
    	at gui.GuiHandler.getServerGuiElement(GuiHandler.java:18) ~[GuiHandler.class:?]
    	at net.minecraftforge.fml.common.network.NetworkRegistry.getRemoteGuiContainer(NetworkRegistry.java:251) ~[NetworkRegistry.class:?]
    	at net.minecraftforge.fml.common.network.internal.FMLNetworkHandler.openGui(FMLNetworkHandler.java:87) ~[FMLNetworkHandler.class:?]
    	at net.minecraft.entity.player.EntityPlayer.openGui(EntityPlayer.java:2723) ~[EntityPlayer.class:?]
    	at blocks.BlockStorageAssembler.onBlockActivated(BlockStorageAssembler.java:47) ~[blockStorageAssembler.class:?]
    	at net.minecraft.server.management.PlayerInteractionManager.processRightClickBlock(PlayerInteractionManager.java:477) ~[PlayerInteractionManager.class:?]
    	at net.minecraft.network.NetHandlerPlayServer.processRightClickBlock(NetHandlerPlayServer.java:706) ~[NetHandlerPlayServer.class:?]
    	at net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock.processPacket(CPacketPlayerTryUseItemOnBlock.java:68) ~[CPacketPlayerTryUseItemOnBlock.class:?]
    	at net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock.processPacket(CPacketPlayerTryUseItemOnBlock.java:13) ~[CPacketPlayerTryUseItemOnBlock.class:?]
    	at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:15) ~[PacketThreadUtil$1.class:?]
    	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_77]
    	at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_77]
    	at net.minecraft.util.Util.runTask(Util.java:25) ~[util.class:?]
    	... 5 more

Container:

   

package container;
    
    public class ContainerStorageAssembler extends Container
    {
    	TileEntityStorageAssembler tile;
    	public ContainerStorageAssembler(IInventory inv, TileEntityStorageAssembler tile)
    	{
    		this.tile = tile;
    		int j;
    		int k;
    		this.addSlotToContainer(new Slot(tile.inventory, 1, 44, 21)); //If I erase this line everything will work fine.
    		
    		int i = -18;        
            for(j = 0; j < 3; j++)
            {
                for (k = 0; k < 9; k++)
                {
                    this.addSlotToContainer(new Slot(inv, k + j * 9 + 9, 8 + k * 18, 102 + j * 18 + i));
                }
            }
     
            for(j = 0; j < 9; j++)
            {
                this.addSlotToContainer(new Slot(inv, j, 8 + j * 18, 160 + i));
            }
    	}
    	
    	@Override
    	public boolean canInteractWith(EntityPlayer playerIn)
    	{
    		return true;
    	}
    }

Tile Entity:

 

 package tileentity;
    
    public class TileEntityStorageAssembler extends TileEntity
    {
    	public InventoryBasic inventory;
    	
    	public TileEntityStorageAssembler()
    	{
    		inventory = new InventoryBasic("StorageAssemblerInventory", false, 10);
    	}
    	
    	@Override
    	public NBTTagCompound writeToNBT(NBTTagCompound compound)
    	{
    		super.writeToNBT(compound);
    		NBTTagList list = new NBTTagList();
    		for(int i = 0; i < inventory.getSizeInventory(); i++)
    		{
    			if(inventory.getStackInSlot(i) != null)
    			{
    				NBTTagCompound tag = new NBTTagCompound();
    				tag.setByte("slot", (byte) i);
    				inventory.getStackInSlot(i).writeToNBT(tag);
    			}
    		}
    		compound.setTag("items", list);
    		return compound;
    	}
    	
    	@Override
    	public void readFromNBT(NBTTagCompound compound)
    	{
    		super.readFromNBT(compound);
    		NBTTagList list = compound.getTagList("items", 10);
    		inventory = new InventoryBasic("StorageAssemblerInventory", false, 10);
    		for(int i = 0; i < list.tagCount(); i++)
    		{
    			NBTTagCompound tag = list.getCompoundTagAt(i);
    			byte b = tag.getByte("slot");
    			if(b >= 0 && b < inventory.getSizeInventory())
    			{
    				inventory.setInventorySlotContents(b, ItemStack.loadItemStackFromNBT(tag));
    			}
    		}
    	}
    }

Gui Handler:

   

package gui;
    
    public class GuiHandler implements IGuiHandler
    {
    	@Override
    	public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)
    	{
    		switch (ID)
    		{
    		case Data.GUI_ASSEMBLER: return new ContainerStorageAssembler(player.inventory, (TileEntityStorageAssembler) world.getTileEntity(new BlockPos(x, y, z)));
    		default: return null;
    		}
    	}
    
    	@Override
    	public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)
    	{
    		switch (ID)
    		{
    		case Data.GUI_ASSEMBLER: return new GuiStorageAssembler(new ContainerStorageAssembler(player.inventory, (TileEntityStorageAssembler) world.getTileEntity(new BlockPos(x, y, z))));
    		default: return null;
    		}
    	}
    }

Gui:

   

package gui;
    
    public class GuiStorageAssembler extends GuiContainer
    {
    	public static final ResourceLocation GUILOC = new ResourceLocation(Data.MODID + ":textures/gui/assembler.png");
    	
    	public GuiStorageAssembler(Container container)
    	{
    		super(container);
    	}
    
    	@Override
    	protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY)
    	{
    		mc.renderEngine.bindTexture(GUILOC);
    		int px = (this.width - this.xSize) / 2;
    		int py = (this.height - this.ySize) / 2;
    		drawTexturedModalRect(px, py, 0, 0, xSize, ySize);
    	}
    	
    	@Override
    	protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY)
    	{
    		int px = this.xSize / 2;
    		String title = I18n.format("container.storageassembler");
    		fontRendererObj.drawString(title, px - (fontRendererObj.getStringWidth(title) / 2), 5, 4210752);
    	}
    }

Block:

   

package blocks;
    
    public class BlockStorageAssembler extends BlockContainer
    {
    	public BlockStorageAssembler()
    	{
    		super(Material.IRON);
    		setUnlocalizedName("storage_assembler");
    		setRegistryName("storage_assembler");
    	}
    
    	@Override
    	public TileEntity createNewTileEntity(World worldIn, int meta)
    	{
    		return new TileEntityStorageAssembler();
    	}
    	
    	@Override
    	public EnumBlockRenderType getRenderType(IBlockState state)
    	{
    		return EnumBlockRenderType.MODEL;
    	}
    	
    	@Override
    	public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ)
    	{
    		if(!worldIn.isRemote)
    		{
    			TileEntity te = worldIn.getTileEntity(pos);
    			if(te instanceof TileEntityStorageAssembler)
    			{
    				playerIn.openGui(ModularStorage.instance, Data.GUI_ASSEMBLER, worldIn, pos.getX(), pos.getY(), pos.getY());
    			}
    		}
    		return true;
    	}
    }

Main:

   

package main;
    
    @Mod(modid=Data.MODID, version=Data.VERSION, name=Data.NAME)
    public class ModularStorage
    {
    	@SidedProxy(serverSide="proxy.ServerProxy", clientSide="proxy.ClientProxy")
    	public static ServerProxy proxy;
    	
    	@Instance
    	public static ModularStorage instance;
    	
    	public static CreativeTabs tabModularStorage = new CreativeTabs("modularStorage")
    	{
    		@Override
    		public Item getTabIconItem()
    		{
    			return itemModuleItems;
    		}
    	};
    	
    	public static ItemModuleItems itemModuleItems = new ItemModuleItems();
    	public static ItemModuleFluids itemModuleFluids = new ItemModuleFluids();
    	public static ItemModuleEnergy itemModuleEnergy = new ItemModuleEnergy();
    	
    	public static BlockStorageAssembler blockStorageAssembler = new BlockStorageAssembler();
    	
    	@EventHandler
    	public void preInit(FMLPreInitializationEvent e)
    	{
    		GameRegistry.registerTileEntity(TileEntityStorageAssembler.class, "TileEntityStorageAssembler");
    		
    		GameRegistry.register(itemModuleItems);
    		GameRegistry.register(itemModuleFluids);
    		GameRegistry.register(itemModuleEnergy);
    		
    		registerBlock(blockStorageAssembler);
    	}
    	
    	@EventHandler
    	public void init(FMLInitializationEvent e)
    	{
    		proxy.registerClient();
    		NetworkRegistry.INSTANCE.registerGuiHandler(instance, new GuiHandler());
    	}
    	
    	@EventHandler
    	public void postInit(FMLPostInitializationEvent e)
    	{
    		
    	}
    	
    	public void registerBlock(Block block)
    	{
    		GameRegistry.register(block);
    		GameRegistry.register(new ItemBlock(block).setRegistryName(block.getRegistryName()));
    	}
    }

 

#Edit: Removed Import lines

Link to comment
Share on other sites

Stacks? If you mean slots it has ten.

Instead of having your TileEntity contain a field of InventoryBasic why not make it implement ISidedInventory(Used to be able to interact with pipes/hoppers) or IInventory.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

I give it a try but I dont know if that will fix it. Usually when I use IInventory I still contain a field in my tileentity and use IInventory just as a bridge. Also I was not planning on making it compatible with hoppers because it will be some sort of crafting table. Even if I could still use canConnect a field felt easier to use.

Link to comment
Share on other sites

Good Jorb :D

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.



×
×
  • Create New...

Important Information

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