Getting a weird NPE when opening a gui...

DonkeyTeeth2013

New Member
Jul 29, 2019
12
0
0
As the title states, there's this odd NullPointerException that crashes my client every time I open a GUI that I made. The stack trace / code is below:

Code:
[15:40:57] [Client thread/FATAL]: Reported exception thrown!
net.minecraft.util.ReportedException: Rendering item
    at net.minecraft.client.renderer.EntityRenderer.updateCameraAndRender(EntityRenderer.java:1167) ~[EntityRenderer.class:?]
    at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1114) ~[Minecraft.class:?]
    at net.minecraft.client.Minecraft.run(Minecraft.java:376) [Minecraft.class:?]
    at net.minecraft.client.main.Main.main(Main.java:117) [Main.class:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_51]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_51]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_51]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_51]
    at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.12.jar:?]
    at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.12.jar:?]
    at net.minecraftforge.gradle.GradleStartCommon.launch(Unknown Source) [start/:?]
    at GradleStart.main(Unknown Source) [start/:?]
Caused by: java.lang.NullPointerException
    at net.minecraft.item.ItemStack.getMetadata(ItemStack.java:280) ~[ItemStack.class:?]
    at net.minecraft.client.renderer.ItemModelMesher.getMetadata(ItemModelMesher.java:70) ~[ItemModelMesher.class:?]
    at net.minecraft.client.renderer.ItemModelMesher.getItemModel(ItemModelMesher.java:43) ~[ItemModelMesher.class:?]
    at net.minecraft.client.renderer.entity.RenderItem.renderItemIntoGUI(RenderItem.java:363) ~[RenderItem.class:?]
    at net.minecraft.client.renderer.entity.RenderItem.renderItemAndEffectIntoGUI(RenderItem.java:414) ~[RenderItem.class:?]
    at codechicken.nei.guihook.GuiContainerManager.drawSlotItem(GuiContainerManager.java:491) ~[GuiContainerManager.class:?]
    at net.minecraft.client.gui.inventory.GuiContainer.drawSlot(GuiContainer.java:292) ~[GuiContainer.class:?]
    at net.minecraft.client.gui.inventory.GuiContainer.drawScreen(GuiContainer.java:116) ~[GuiContainer.class:?]
    at net.minecraftforge.client.ForgeHooksClient.drawScreen(ForgeHooksClient.java:460) ~[ForgeHooksClient.class:?]
    at net.minecraft.client.renderer.EntityRenderer.updateCameraAndRender(EntityRenderer.java:1137) ~[EntityRenderer.class:?]
    ... 11 more
[15:40:57] [Client thread/INFO] [STDOUT]: [net.minecraft.init.Bootstrap:printToSYSOUT:663]: ---- Minecraft Crash Report ----

WARNING: coremods are present:
  CCLCorePlugin (CodeChickenLib-1.8-1.1.2.133-dev.jar)
  CodeChickenCorePlugin (CodeChickenCore-1.8-1.0.5.36-universal.jar)
  CCCDeobfPlugin (unknown)
  NEICorePlugin (NotEnoughItems-1.8-1.0.5.104-universal.jar)
  BOPLoadingPlugin (BiomesOPlenty-1.8-3.0.0.1377-universal.jar)
Contact their authors BEFORE contacting forge

// Oh - I know what I did wrong!

Time: 8/14/15 3:40 PM
Description: Rendering item

java.lang.NullPointerException: Rendering item
    at net.minecraft.item.ItemStack.getMetadata(ItemStack.java:280)
    at net.minecraft.client.renderer.ItemModelMesher.getMetadata(ItemModelMesher.java:70)
    at net.minecraft.client.renderer.ItemModelMesher.getItemModel(ItemModelMesher.java:43)
    at net.minecraft.client.renderer.entity.RenderItem.renderItemIntoGUI(RenderItem.java:363)
    at net.minecraft.client.renderer.entity.RenderItem.renderItemAndEffectIntoGUI(RenderItem.java:414)
    at codechicken.nei.guihook.GuiContainerManager.drawSlotItem(GuiContainerManager.java:491)
    at net.minecraft.client.gui.inventory.GuiContainer.drawSlot(GuiContainer.java:292)
    at net.minecraft.client.gui.inventory.GuiContainer.drawScreen(GuiContainer.java:116)
    at net.minecraftforge.client.ForgeHooksClient.drawScreen(ForgeHooksClient.java:460)
    at net.minecraft.client.renderer.EntityRenderer.updateCameraAndRender(EntityRenderer.java:1137)
    at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1114)
    at net.minecraft.client.Minecraft.run(Minecraft.java:376)
    at net.minecraft.client.main.Main.main(Main.java:117)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
    at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
    at net.minecraftforge.gradle.GradleStartCommon.launch(Unknown Source)
    at GradleStart.main(Unknown Source)


A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- Head --
Stacktrace:
    at net.minecraft.item.ItemStack.getMetadata(ItemStack.java:280)
    at net.minecraft.client.renderer.ItemModelMesher.getMetadata(ItemModelMesher.java:70)
    at net.minecraft.client.renderer.ItemModelMesher.getItemModel(ItemModelMesher.java:43)
    at net.minecraft.client.renderer.entity.RenderItem.renderItemIntoGUI(RenderItem.java:363)

-- Item being rendered --
Details:
    Item Type: null
    Item Aux: ~~ERROR~~ NullPointerException: null
    Item NBT: null
    Item Foil: ~~ERROR~~ NullPointerException: null
Stacktrace:
    at net.minecraft.client.renderer.entity.RenderItem.renderItemAndEffectIntoGUI(RenderItem.java:414)
    at codechicken.nei.guihook.GuiContainerManager.drawSlotItem(GuiContainerManager.java:491)
    at net.minecraft.client.gui.inventory.GuiContainer.drawSlot(GuiContainer.java:292)
    at net.minecraft.client.gui.inventory.GuiContainer.drawScreen(GuiContainer.java:116)
    at net.minecraftforge.client.ForgeHooksClient.drawScreen(ForgeHooksClient.java:460)

-- Screen render details --
Details:
    Screen name: donkeycore.fusion.containers.GUIVolatileBlock
    Mouse location: Scaled: (420, 249). Absolute: (840, 499)
    Screen size: Scaled: (840, 499). Absolute: (1680, 998). Scale factor of 2

-- Affected level --
Details:
    Level name: MpServer
    All players: 1 total; [EntityPlayerSP['Player731'/103, l='MpServer', x=179.28, y=3.00, z=283.58]]
    Chunk stats: MultiplayerChunkCache: 624, 624
    Level seed: 0
    Level generator: ID 00 - default, ver 1. Features enabled: false
    Level generator options:
    Level spawn location: 132.00,64.00,136.00 - World: (132,64,136), Chunk: (at 4,4,8 in 8,8; contains blocks 128,0,128 to 143,255,143), Region: (0,0; contains chunks 0,0 to 31,31, blocks 0,0,0 to 511,255,511)
    Level time: 373015 game time, 6000 day time
    Level dimension: 0
    Level storage version: 0x00000 - Unknown?
    Level weather: Rain time: 0 (now: false), thunder time: 0 (now: false)
    Level game mode: Game mode: creative (ID 1). Hardcore: false. Cheats: false
    Forced entities: 64 total; [*snip*]
    Retry entities: 0 total; []
    Server brand: fml,forge
    Server type: Integrated singleplayer server
Stacktrace:
    at net.minecraft.client.multiplayer.WorldClient.addWorldInfoToCrashReport(WorldClient.java:392)
    at net.minecraft.client.Minecraft.addGraphicsAndWorldToCrashReport(Minecraft.java:2614)
    at net.minecraft.client.Minecraft.run(Minecraft.java:398)
    at net.minecraft.client.main.Main.main(Main.java:117)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
    at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
    at net.minecraftforge.gradle.GradleStartCommon.launch(Unknown Source)
    at GradleStart.main(Unknown Source)

-- System Details --
Details:
    Minecraft Version: 1.8
    Operating System: Linux (amd64) version 3.16.0-38-generic
    Java Version: 1.8.0_51, Oracle Corporation
    Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation
    Memory: 640513696 bytes (610 MB) / 1037959168 bytes (989 MB) up to 1037959168 bytes (989 MB)
    JVM Flags: 3 total; -Xincgc -Xmx1024M -Xms1024M
    IntCache: cache: 0, tcache: 0, allocated: 13, tallocated: 95
    FML: MCP v9.10 FML v8.0.99.99 Minecraft Forge 11.14.3.1501 8 mods loaded, 8 mods active
    States: 'U' = Unloaded 'L' = Loaded 'C' = Constructed 'H' = Pre-initialized 'I' = Initialized 'J' = Post-initialized 'A' = Available 'D' = Disabled 'E' = Errored
    UCHIJAAAA    mcp{9.05} [Minecraft Coder Pack] (minecraft.jar)
    UCHIJAAAA    FML{8.0.99.99} [Forge Mod Loader] (forgeSrc-1.8-11.14.3.1501.jar)
    UCHIJAAAA    Forge{11.14.3.1501} [Minecraft Forge] (forgeSrc-1.8-11.14.3.1501.jar)
    UCHIJAAAA    CodeChickenCore{1.0.5.36} [CodeChicken Core] (minecraft.jar)
    UCHIJAAAA    NotEnoughItems{1.0.5.104} [Not Enough Items] (NotEnoughItems-1.8-1.0.5.104-universal.jar)
    UCHIJAAAA    fusion{0.8} [Fusion] (bin)
    UCHIJAAAA    BiomesOPlenty{3.0.0} [Biomes O' Plenty] (BiomesOPlenty-1.8-3.0.0.1377-universal.jar)
    UCHIJAAAA    Waila{1.6.0_B1} [Waila] (Waila-1.6.0_B1_1.8.1.jar)
    Loaded coremods (and transformers):
CCLCorePlugin (CodeChickenLib-1.8-1.1.2.133-dev.jar)
  codechicken.lib.asm.ClassHeirachyManager
  codechicken.lib.asm.RenderHookTransformer
CodeChickenCorePlugin (CodeChickenCore-1.8-1.0.5.36-universal.jar)
  codechicken.core.asm.InterfaceDependancyTransformer
  codechicken.core.asm.TweakTransformer
  codechicken.core.asm.DelegatedTransformer
  codechicken.core.asm.DefaultImplementationTransformer
CCCDeobfPlugin (unknown)
 
NEICorePlugin (NotEnoughItems-1.8-1.0.5.104-universal.jar)
  codechicken.nei.asm.NEITransformer
BOPLoadingPlugin (BiomesOPlenty-1.8-3.0.0.1377-universal.jar)
 
    GL info: ' Vendor: 'NVIDIA Corporation' Version: '4.5.0 NVIDIA 346.72' Renderer: 'GeForce GTX 750 Ti/PCIe/SSE2'
    Launched Version: 1.8
    LWJGL: 2.9.1
    OpenGL: GeForce GTX 750 Ti/PCIe/SSE2 GL version 4.5.0 NVIDIA 346.72, NVIDIA Corporation
    GL Caps: Using GL 1.3 multitexturing.
Using GL 1.3 texture combiners.
Using framebuffer objects because OpenGL 3.0 is supported and separate blending is supported.
Shaders are available because OpenGL 2.1 is supported.
VBOs are available because OpenGL 1.5 is supported.

    Using VBOs: No
    Is Modded: Definitely; Client brand changed to 'fml,forge'
    Type: Client (map_client.txt)
    Resource Packs: []
    Current Language: English (US)
    Profiler Position: N/A (disabled)
[15:40:57] [Client thread/INFO] [STDOUT]: [net.minecraft.init.Bootstrap:printToSYSOUT:663]: #@!@# Game crashed! Crash report saved to: #@!@# /Removed/eclipse/./crash-reports/crash-2015-08-14_15.40.57-client.txt

As you can see, it strangely is caused by ItemStack.getMetadata(), which will throw the NPE because it doesn't perform a null check on its item field. I don't know how to get around this, but here is my code:

VolatileBlock.java:
Code:
package donkeycore.fusion.blocks;

import java.util.Random;

import com.google.common.base.Predicate;

import donkeycore.fusion.Fusion;
import donkeycore.fusion.IFusionObject;
import donkeycore.fusion.items.materials.VolatileMatter;
import donkeycore.fusion.tileentities.TileEntityVolatileBlock;
import net.minecraft.block.Block;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.Vec3i;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.oredict.OreDictionary;
import net.minecraftforge.oredict.ShapedOreRecipe;

public class VolatileBlock extends Block implements IFusionObject, ITileEntityProvider {
   
    public static final String name = "volatile_block";
    public static final String oreName = "blockVolatile";
    public static final PropertyEnum ACTIVATED = PropertyEnum.create("activated", Activated.class, new Predicate<Activated>() {
       
        @Override
        public boolean apply(Activated input) {
            return true;
        }
       
    });
    private int radius = 0;
   
    public VolatileBlock() {
        super(Material.clay);
        setStepSound(Block.soundTypeCloth);
        setHardness(1F);
        setLightLevel(7F);
        setTickRandomly(true);
        setCreativeTab(Fusion.tabFusion);
        setUnlocalizedName(name);
        setDefaultState(getDefaultState().withProperty(ACTIVATED, Activated.FALSE));
    }
   
    @Override
    public TileEntity createTileEntity(World w, IBlockState state) {
        TileEntityVolatileBlock tile = new TileEntityVolatileBlock();
        tile.radius = radius;
        return tile;
    }
   
    @Override
    public TileEntity createNewTileEntity(World w, int meta) {
        return createTileEntity(w, getStateFromMeta(meta));
    }
   
    @Override
    public boolean onBlockActivated(World w, BlockPos pos, IBlockState state, EntityPlayer p, EnumFacing side, float hitX, float hitY, float hitZ) {
        if (p.isSneaking()) {
            boolean activated = isActivated(state);
            radius = ((TileEntityVolatileBlock) w.getTileEntity(pos)).radius;
            w.setBlockState(pos, getDefaultState().withProperty(ACTIVATED, activated ? Activated.FALSE : Activated.TRUE));
            return true;
        } else {
            TileEntity tileentity = w.getTileEntity(pos);
            /*for(ItemStack i : ((TileEntityVolatileBlock) w.getTileEntity(pos)).items) {
                if(i == null) {
                    System.out.println("NULL");
                    continue;
                }
                System.out.println(i.stackSize + "x");
                System.out.println(i.getItem());
                System.out.println(i.getMetadata()); //<-- NPE will occur here
            }*/
            if (tileentity instanceof TileEntityVolatileBlock)
                p.openGui(Fusion.instance, 0, w, pos.getX(), pos.getY(), pos.getZ()); //<-- NPE will occur here
        }
        return super.onBlockActivated(w, pos, state, p, side, hitX, hitY, hitZ);
    }
   
    public boolean isActivated(IBlockState state) {
        return Activated.from(state).value;
    }
   
    @Override
    public int getMetaFromState(IBlockState state) {
        return Activated.from(state).value ? 1 : 0;
    }
   
    @Override
    public IBlockState getStateFromMeta(int meta) {
        if (meta > 1)
            meta = 0;
        return getBlockState().getBaseState().withProperty(ACTIVATED, Activated.from(meta == 1));
    }
   
    @Override
    protected BlockState createBlockState() {
        return new BlockState(this, new IProperty[]{ACTIVATED});
    }
   
    @Override
    public void updateTick(World w, BlockPos pos, IBlockState state, Random r) {
        if (!w.isRemote && r.nextInt(5000) != 0) {
            for (Object o : w.getEntitiesWithinAABB(EntityPlayer.class, new AxisAlignedBB(pos.subtract(new Vec3i(3, 3, 3)), pos.add(new Vec3i(3, 3, 3))))) {
                if (o instanceof EntityPlayer && o != null) {
                    EntityPlayer e = (EntityPlayer) o;
                    if (!e.capabilities.isCreativeMode)
                        e.attackEntityFrom(Fusion.sourceRadiation, 10F);
                }
            }
        }
    }
   
    @Override
    public void preinit() {
        GameRegistry.registerBlock(this, name);
        OreDictionary.registerOre(oreName, this);
        GameRegistry.addRecipe(new ShapedOreRecipe(new ItemStack(this), "VVV", "VVV", "VVV", 'V', VolatileMatter.oreName));
    }
   
    @Override
    public void init() {
        if (Fusion.SIDE == Side.CLIENT)
            Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(GameRegistry.findItem(Fusion.MODID, name), 0, new ModelResourceLocation(Fusion.MODID + ":" + name, "inventory"));
    }
   
    public static enum Activated implements IStringSerializable {
       
        FALSE(false, "false"), TRUE(true, "true");
       
        public final boolean value;
        public final String name;
       
        private Activated(boolean stage, String name) {
            this.value = stage;
            this.name = name;
        }
       
        @Override
        public String getName() {
            return name;
        }
       
        public static boolean has(IBlockState state) {
            try {
                return from(state).value;
            } catch (Throwable t) {
                return false;
            }
        }
       
        public static Activated from(IBlockState state) {
            return ((Activated) state.getValue(ACTIVATED));
        }
       
        public static Activated from(boolean value) {
            for (Activated s : values()) {
                if (value == s.value)
                    return s;
            }
            return Activated.FALSE;
        }
       
    }
   
}

TileEntityVolatileBlock.java:
Code:
package donkeycore.fusion.tileentities;

import donkeycore.fusion.Fusion;
import donkeycore.fusion.blocks.DegradedFusionBlock;
import donkeycore.fusion.blocks.VolatileBlock;
import donkeycore.fusion.containers.ContainerVolatileBlock;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.server.gui.IUpdatePlayerListBox;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.IChatComponent;
import net.minecraft.world.IInteractionObject;
import net.minecraft.world.World;

public class TileEntityVolatileBlock extends TileEntity implements IInteractionObject, IInventory, IUpdatePlayerListBox {
   
    public static final int ROWS = 9;
    public static final int COLUMNS = 12;
    public ItemStack[] items = new ItemStack[ROWS * COLUMNS];
    public int radius = 0;
   
    @Override
    public void update() {
        fixInventory();
        World w = worldObj;
        if(!w.isRemote && !isInventoryFull()) {
            BlockPos bp = getPos();
            boolean activated = ((VolatileBlock) w.getBlockState(bp).getBlock()).isActivated(w.getBlockState(bp));
            if(!activated)
                return;
            if(w.rand.nextInt(100) == 0)
                radius++;
            for(int x = bp.getX() - radius; x <= bp.getX() + radius; x++) {
                for(int y = bp.getY() - radius; y <= bp.getY() + radius; y++) {
                    if(y < 1)
                        continue;
                    for(int z = bp.getZ() - radius; z <= bp.getZ() + radius; z++) {
                        BlockPos pos = new BlockPos(x, y, z);
                        IBlockState state = w.getBlockState(pos);
                        if(state != null && state.getBlock() != null && state.getBlock() != Blocks.air && state.getBlock() != Blocks.bedrock && !(state.getBlock() instanceof DegradedFusionBlock) && !(state.getBlock() instanceof ITileEntityProvider) && w.rand.nextInt(100) == 0)  {
                            boolean flag = true;
                            for(int i = 0; i < items.length; i++) {
                                ItemStack is = items[i];
                                if(is != null && is.stackSize < 64 && is.getItem() != null && is.getItem() == Item.getItemFromBlock(state.getBlock()) && is.getMetadata() == state.getBlock().getMetaFromState(state)) {
                                    w.setBlockToAir(pos);
                                    is.stackSize++;
                                    flag = false;
                                    break;
                                }
                            }
                            if(flag) { //true if item needs to be added
                                if(isInventoryFull())
                                    return;
                                else {
                                    w.setBlockToAir(pos);
                                    for(int i = 0; i < items.length; i++) {
                                        ItemStack is = items[i];
                                        if(is == null) {
                                            items[i] = new ItemStack(state.getBlock(), 1, state.getBlock().getMetaFromState(state));
                                            flag = false;
                                            break;
                                        }
                                    }
                                }
                            }
                            markDirty();
                        }
                    }
                }
            }
        }
    }
   
    private void fixInventory() {
        for(int i = 0; i < items.length; i++) {
            if(items[i] == null || items[i].getItem() == null)
                items[i] = new ItemStack(Blocks.air);
        }
    }
   
    private boolean isInventoryFull() {
        for(int i = 0; i < items.length; i++) {
            if(items[i] == null)
                return false;
        }
        return true;
    }

    @Override
    public String getName() {
        return "container.volatile_block";
    }
   
    @Override
    public boolean hasCustomName() {
        return false;
    }
   
    @Override
    public IChatComponent getDisplayName() {
        return new ChatComponentText("Volatile Block");
    }
   
    @Override
    public int getSizeInventory() {
        return items.length;
    }
   
    @Override
    public ItemStack getStackInSlot(int index) {
        return items[index];
    }
   
    @Override
    public ItemStack decrStackSize(int index, int count) {
        if (items[index] != null) {
            if (items[index].stackSize <= count) {
                ItemStack itemstack = items[index];
                items[index] = null;
                return itemstack;
            } else {
                ItemStack itemstack = items[index].splitStack(count);
                if (items[index].stackSize == 0)
                    items[index] = null;
                return itemstack;
            }
        } else
            return null;
    }
   
    @Override
    public ItemStack getStackInSlotOnClosing(int index) {
        if (items[index] != null) {
            ItemStack itemstack = items[index];
            items[index] = null;
            return itemstack;
        } else
            return null;
    }
   
    @Override
    public void setInventorySlotContents(int index, ItemStack stack) {
        boolean flag = stack != null && stack.isItemEqual(items[index]) && ItemStack.areItemStackTagsEqual(stack, items[index]);
        items[index] = stack;
       
        if (stack != null && stack.stackSize > this.getInventoryStackLimit())
            stack.stackSize = this.getInventoryStackLimit();
           
        if (!flag)
            this.markDirty();
    }
   
    @Override
    public int getInventoryStackLimit() {
        return 64;
    }
   
    @Override
    public boolean isUseableByPlayer(EntityPlayer player) {
        return this.worldObj.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double) this.pos.getX() + 0.5D, (double) this.pos.getY() + 0.5D, (double) this.pos.getZ() + 0.5D) <= 64.0D;
    }
   
    @Override
    public void readFromNBT(NBTTagCompound compound) {
        super.readFromNBT(compound);
        NBTTagList list = compound.getTagList("Items", 10);
        items = new ItemStack[this.getSizeInventory()];
        for (int i = 0; i < list.tagCount(); ++i) {
            NBTTagCompound tag = list.getCompoundTagAt(i);
            byte slot = tag.getByte("Slot");
            if (slot >= 0 && slot < items.length)
                items[slot] = ItemStack.loadItemStackFromNBT(tag);
        }
        radius = compound.getInteger("radius");
    }
   
    @Override
    public void writeToNBT(NBTTagCompound compound) {
        super.writeToNBT(compound);
        compound.setInteger("radius", radius);
        NBTTagList list = new NBTTagList();
        for (int slot = 0; slot < items.length; ++slot) {
            if (items[slot] != null) {
                NBTTagCompound tag = new NBTTagCompound();
                tag.setByte("Slot", (byte) slot);
                items[slot].writeToNBT(tag);
                list.appendTag(tag);
            }
        }
        compound.setTag("Items", list);
    }
   
    @Override
    public void openInventory(EntityPlayer player) {}
   
    @Override
    public void closeInventory(EntityPlayer player) {}
   
    @Override
    public boolean isItemValidForSlot(int index, ItemStack stack) {
        return false;
    }
   
    public int getField(int id) {
        switch (id) {
            case 0:
                return radius;
            default:
                return 0;
        }
    }
   
    public void setField(int id, int value) {
        switch (id) {
            case 0:
                this.radius = value;
        }
    }
   
    public int getFieldCount() {
        return 1;
    }
   
    @Override
    public void clear() {
        for (int i = 0; i < items.length; ++i)
            items[i] = null;
    }
   
    @Override
    public Container createContainer(InventoryPlayer i, EntityPlayer p) {
        return new ContainerVolatileBlock(i, this);
    }
   
    @Override
    public String getGuiID() {
        return VolatileBlock.name;
    }
   
}

ContainerVolatileBlock.java:
Code:
package donkeycore.fusion.containers;

import static donkeycore.fusion.tileentities.TileEntityVolatileBlock.COLUMNS;
import static donkeycore.fusion.tileentities.TileEntityVolatileBlock.ROWS;

import donkeycore.fusion.tileentities.TileEntityVolatileBlock;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public class ContainerVolatileBlock extends Container {
   
    private TileEntityVolatileBlock tileVolatileBlock;
   
    public ContainerVolatileBlock(InventoryPlayer player, TileEntityVolatileBlock tileEntityVolatileBlock) {
        this.tileVolatileBlock = tileEntityVolatileBlock;
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLUMNS; j++)
                addSlotToContainer(new VolatileBlockSlot(tileVolatileBlock, i * COLUMNS + j, 12 + j * 18, 8 + i * 18));
        }
       
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 9; j++)
                addSlotToContainer(new Slot(player, j + i * 9 + 9, 39 + j * 18, 256 - (4 - i) * 18 - 10));
               
        }
        for (int i = 0; i < 9; i++)
            addSlotToContainer(new Slot(player, i, 39 + i * 18, 232));
    }
   
    @Override
    public boolean canInteractWith(EntityPlayer playerIn) {
        return tileVolatileBlock.isUseableByPlayer(playerIn);
    }
   
    @Override
    public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) {
        return null;
    }
   
}

So yeah, I know the NPE comes from ItemStack.getMetadata(), but I don't really know much other than that or how to fix this entire thing. What's wrong here?
 

DonkeyTeeth2013

New Member
Jul 29, 2019
12
0
0
I managed to fix it by iterating through the inventory and setting all indexes to null if the itemstack isn't null but the item contained in it is. Not the best fix, but it works for now.
 

Tbsc

New Member
Jul 29, 2019
22
0
0
You are checking to see if the itemstack is null, and if its null, THEN get the metadata. Meaning, in the null check, change continue; to break;

Sent from my phone to this forum
 

DonkeyTeeth2013

New Member
Jul 29, 2019
12
0
0
<mini rant>

Then add it back, and do what he says.

No, you guys aren't understanding. That is debug code (as blatantly indicated by the System.out.println() statements). Added after I got the NPE and when I was trying to figure out what was wrong. I added it back with a null check sometime between August 14-15 (before Tbsc commented), and of course the code still didn't work, because it was debug code.

If you read the method entirely, you would see that I had 2 instances of "//<-- NPE will occur here". One was in the debug code, and one was in the actual code. The debug code was added in the first place because if I followed the openGui method, it eventually lead to the NPE from a bad call to ItemStack.getMetadata(), which gives an NPE because the ItemStack will try to access a null Item to get its metadata.

Additionally, when you say to add it back, note that the NPE is occurring even when the code is commented out (the commented code wouldn't even be there if it worked in the first place). So adding the commented out code back would do nothing. Adding a check in the debug code would move the NPE from happening in the debug code to happening at the original openGui method that I had before. So it wouldn't even make sense to add the debug code and add a check for that. I'm not trying to fix the debug code, I'm trying to fix the actual problem.

So, instead of telling me to do things when you didn't understand the code beforehand, maybe you should read all of the code first.

</mini rant>

Sorry for the rant or if I sounded rude, but I just felt like I needed to get that out there (also having some IRL difficulties so I'm not in my happiest mood). Either way, the code works (as mentioned in the 2nd reply of this post, where I said I fixed it by iterating through the items and so on), so continuing to fix this problem isn't exactly on the top of my agenda. Thanks anyway.
 
  • Like
Reactions: Padfoote