Jump to content

Gui throwing null pointer expression


knoxi

Recommended Posts

Ok, so I've been working on a gui and I've gotten most of it to work but just as I am finishing an error appeared,

Whenever I right click the block instead of showing the gui minecraft crashes.

this is the error message: 

Quote

java.lang.NullPointerException: Ticking entity
    at java.util.Objects.requireNonNull(Objects.java:208) ~[?:?] {}
    at net.minecraft.world.inventory.AbstractContainerMenu.broadcastChanges(AbstractContainerMenu.java:169) ~[forge-1.19-41.0.107_mapped_official_1.19-recomp.jar%23181!/:?] {re:classloading}
    at net.minecraft.server.level.ServerPlayer.tick(ServerPlayer.java:414) ~[forge-1.19-41.0.107_mapped_official_1.19-recomp.jar%23181!/:?] {re:classloading,pl:accesstransformer:B}
    at net.minecraft.server.level.ServerLevel.tickNonPassenger(ServerLevel.java:657) ~[forge-1.19-41.0.107_mapped_official_1.19-recomp.jar%23181!/:?] {re:classloading,pl:accesstransformer:B}
    at net.minecraft.world.level.Level.guardEntityTick(Level.java:457) ~[forge-1.19-41.0.107_mapped_official_1.19-recomp.jar%23181!/:?] {re:classloading,pl:accesstransformer:B}
    at net.minecraft.server.level.ServerLevel.lambda$tick$3(ServerLevel.java:322) ~[forge-1.19-41.0.107_mapped_official_1.19-recomp.jar%23181!/:?] {re:classloading,pl:accesstransformer:B}
    at net.minecraft.world.level.entity.EntityTickList.forEach(EntityTickList.java:53) ~[forge-1.19-41.0.107_mapped_official_1.19-recomp.jar%23181!/:?] {re:classloading}
    at net.minecraft.server.level.ServerLevel.tick(ServerLevel.java:302) ~[forge-1.19-41.0.107_mapped_official_1.19-recomp.jar%23181!/:?] {re:classloading,pl:accesstransformer:B}
    at net.minecraft.server.MinecraftServer.tickChildren(MinecraftServer.java:866) ~[forge-1.19-41.0.107_mapped_official_1.19-recomp.jar%23181!/:?] {re:classloading,pl:accesstransformer:B}
    at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:806) ~[forge-1.19-41.0.107_mapped_official_1.19-recomp.jar%23181!/:?] {re:classloading,pl:accesstransformer:B}
    at net.minecraft.client.server.IntegratedServer.tickServer(IntegratedServer.java:84) ~[forge-1.19-41.0.107_mapped_official_1.19-recomp.jar%23181!/:?] {re:classloading,pl:runtimedistcleaner:A}
    at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:654) ~[forge-1.19-41.0.107_mapped_official_1.19-recomp.jar%23181!/:?] {re:classloading,pl:accesstransformer:B}
    at net.minecraft.server.MinecraftServer.lambda$spin$2(MinecraftServer.java:245) ~[forge-1.19-41.0.107_mapped_official_1.19-recomp.jar%23181!/:?] {re:classloading,pl:accesstransformer:B}
    at java.lang.Thread.run(Thread.java:833) ~[?:?] {}
 

and this is my code:

ExampleChestScreen.java:

public class ExampleChestScreen extends AbstractContainerScreen<ExampleChestContainer> {
    private static final ResourceLocation TEXTURE = new ResourceLocation(KnoxiItems.MOD_ID,
            "textures/gui/magical_workbench_gui.png");

    private ExtendedButton beanButton;

    public ExampleChestScreen(ExampleChestContainer container, Inventory playerInv, Component title) {
        super(container, playerInv, title);
        this.leftPos = 0;
        this.topPos = 0;
    }

    @Override
    public void render(PoseStack stack, int mouseX, int mouseY, float partialTicks) {
        super.render(stack, mouseX, mouseY, partialTicks);
        this.font.draw(stack, this.title, this.leftPos + 20, this.topPos + 5, 0x404040);
        this.font.draw(stack, this.playerInventoryTitle, this.leftPos + 8, this.topPos + 75, 0x404040);
    }

    @Override
    protected void init() {
        super.init();
        this.beanButton = addRenderableWidget(
                new ExtendedButton(this.leftPos, this.topPos, 16, 16, Component.literal("beans"),
                        btn -> Minecraft.getInstance().player.displayClientMessage(Component.literal("beans"), false)));
    }

    @Override
    protected void renderBg(PoseStack stack, float mouseX, int mouseY, int partialTicks) {
        renderBackground(stack);
        RenderSystem.setShader(GameRenderer::getPositionTexShader);
        RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f);
        RenderSystem.setShaderTexture(0, TEXTURE);
        blit(stack, this.leftPos, this.topPos, 0, 0, this.imageWidth, this.imageHeight);
    }

    @Override
    protected void renderLabels(PoseStack stack, int mouseX, int mouseY) {
    }
}

ExampleChestBlockEntity.java:

public class ExampleChestBlockEntity extends BlockEntity {
    public static Component TITLE = Component.literal("Example Chest");
    IItemHandler inventory;
    public ExampleChestBlockEntity(BlockPos pos, BlockState state) {
        super(ModBlockEntities.EXAMPLE_CHEST.get(), pos, state);
        inventory = new IItemHandler() {
            private ItemStack[] Inventory = new ItemStack[27];
            @Override
            public int getSlots() {
                return Inventory.length;
            }

            @Override
            public @NotNull ItemStack getStackInSlot(int slot) {
                return Inventory[slot];
            }

            @Override
            public @NotNull ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) {
                Inventory[slot] = stack;
                return Inventory[slot];
            }

            @Override
            public @NotNull ItemStack extractItem(int slot, int amount, boolean simulate) {
                if (!(amount >= Inventory[slot].getCount())) {
                    Inventory[slot] = new ItemStack(Inventory[slot].getItem(), Inventory[slot].getCount() - amount);
                } else {
                    Inventory[slot] = null;
                }
                return Inventory[slot];
            }

            @Override
            public int getSlotLimit(int slot) {
                return 64;
            }

            @Override
            public boolean isItemValid(int slot, @NotNull ItemStack stack) {
                return true;
            }
        };
    }
}

ExampleChestContainer.java:

public class ExampleChestContainer extends AbstractContainerMenu {
    private final ContainerLevelAccess containerAccess;

    // Client Constructor
    public ExampleChestContainer(int id, Inventory playerInv) {
        this(id, playerInv, new ItemStackHandler(27), BlockPos.ZERO);
    }

    // Server constructor
    public ExampleChestContainer(int id, Inventory playerInv, IItemHandler slots, BlockPos pos) {
        super(ModBlockContainers.EXAMPLE_CHEST.get(), id);
        this.containerAccess = ContainerLevelAccess.create(playerInv.player.level, pos);

        final int slotSizePlus2 = 18, startX = 8, startY = 86, hotbarY = 144, inventoryY = 18;

        for (int row = 0; row < 3; row++) {
            for (int column = 0; column < 9; column++) {
                addSlot(new SlotItemHandler(slots, row * 9 + column, startX + column * slotSizePlus2,
                        inventoryY + row * slotSizePlus2));
            }
        }

        for (int row = 0; row < 3; row++) {
            for (int column = 0; column < 9; column++) {
                addSlot(new Slot(playerInv, 9 + row * 9 + column, startX + column * slotSizePlus2,
                        startY + row * slotSizePlus2));
            }
        }

        for (int column = 0; column < 9; column++) {
            addSlot(new Slot(playerInv, column, startX + column * slotSizePlus2, hotbarY));
        }
    }

    @Override
    public ItemStack quickMoveStack(Player player, int index) {
        var retStack = ItemStack.EMPTY;
        final Slot slot = getSlot(index);
        if (slot.hasItem()) {
            final ItemStack item = slot.getItem();
            retStack = item.copy();
            if (index < 27) {
                if (!moveItemStackTo(item, 27, this.slots.size(), true))
                    return ItemStack.EMPTY;
            } else if (!moveItemStackTo(item, 0, 27, false))
                return ItemStack.EMPTY;

            if (item.isEmpty()) {
                slot.set(ItemStack.EMPTY);
            } else {
                slot.setChanged();
            }
        }

        return retStack;
    }

    @Override
    public boolean stillValid(Player player) {
        return stillValid(this.containerAccess, player, ModBlocks.EXAMPLE_CHEST.get());
    }

    public static MenuConstructor getServerContainer(ExampleChestBlockEntity chest, BlockPos pos) {
        return (id, playerInv, player) -> new ExampleChestContainer(id, playerInv, chest.inventory, pos);
    }
}

ExampleChestBlock.java:

public class ExampleChestBlock extends HorizontalDirectionalBlock implements EntityBlock {
    public ExampleChestBlock(Properties properties) {
        super(properties);
        registerDefaultState(defaultBlockState().setValue(FACING, Direction.NORTH));
    }

    @Override
    public BlockState getStateForPlacement(BlockPlaceContext context) {
        return defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite());
    }

   /* @Override
    public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state,
                                                                  BlockEntityType<T> beType) {
        //return level.isClientSide ? null
        //        : (level0, pos, state0, blockEntity) -> ((ExampleChestBlockEntity) blockEntity).tick();
        return super.defaultBlockState().getTicker(level, beType);
    }*/

    @Nullable
    @Override
    public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level p_153212_, BlockState p_153213_, BlockEntityType<T> p_153214_) {
        return EntityBlock.super.getTicker(p_153212_, p_153213_, p_153214_);
    }

    @Override
    public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
        return new ExampleChestBlockEntity(pos, state);
    }

    @Override
    public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand,
                                 BlockHitResult result) {
        if (!level.isClientSide && level.getBlockEntity(pos) instanceof final ExampleChestBlockEntity chest) {
            final MenuProvider container = new SimpleMenuProvider(ExampleChestContainer.getServerContainer(chest, pos),
                    ExampleChestBlockEntity.TITLE);
            NetworkHooks.openScreen((ServerPlayer) player, container, pos);
        }

        return InteractionResult.SUCCESS;
    }

    @Override
    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
        super.createBlockStateDefinition(builder);
        builder.add(FACING);
    }
}

Thank you in advance for any help

Link to comment
Share on other sites

Look at the code where it crashes. It has an itemStack::copy of the item stack in each slot.

The error says one of your slots contains a null, when you should be using ItemStack.EMPTY for unoccupied slots.

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

The vanilla chest uses 

Quote

   private NonNullList<ItemStack> items = NonNullList.withSize(27, ItemStack.EMPTY);
 

For its items.

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • https://gist.github.com/it-is-allie/29645c4fb5c7131ad30181769a37d12f There is my latest crash report. I've spent probably 5 hours messing with modpacks and have gotten it almost complete but it then randomly crashes before loading all the mods? I'm not even sure. if anyone  could help it would be greatly appreciated.   
    • Hi, Forge refuses to recognize the mods "moonlight" and "enhancedcelestial" in my friend's modpack for essential, we have double checked that the modpack we're using has both those mods, and it still won't recognize, saying I don't have them. If I try to manually add these mods into my game, it gives me the "Unexpected custom data from client" error. I am stuck in a loop here. -- Unexpected custom data from client Missing required datapack registries: moonlight:soft_fluids, enchancedcelestials:lunar/event, enhancedcelestials:lunar/dimension_settings, moonlight:map_markers -- Logs https://pastebin.com/rvFnk4n3
    • Good afternoon, I have a problem with the minecraft launcher, when trying to open a version with forge it does not open the minecraft and the launcher gives me an error code 1, this passes me from version 1.17 onwards and without having any mod installed.   [29jun.2024 18:08:14.073] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher running: args [--username, MarkitoPlex, --version, 1.20.1-forge-47.3.1, --gameDir, C:\Users\Marco Antonio RL\AppData\Roaming\.minecraft, --assetsDir, C:\Users\Marco Antonio RL\AppData\Roaming\.minecraft\assets, --assetIndex, 5, --uuid, 4768b6d7e7fa48f58946cbe18989d2e3, --accessToken, ????????, --clientId, ZmEwYTkyM2YtZTU2YS00NTVlLWE4NTgtYjY0MWI5YzFhODc1, --xuid, 2535453534635465, --userType, msa, --versionType, release, --quickPlayPath, C:\Users\Marco Antonio RL\AppData\Roaming\.minecraft\quickPlay\java\1719706091955.json, --launchTarget, forgeclient, --fml.forgeVersion, 47.3.1, --fml.mcVersion, 1.20.1, --fml.forgeGroup, net.minecraftforge, --fml.mcpVersion, 20230612.114412] [29jun.2024 18:08:14.077] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher 10.0.9+10.0.9+main.dcd20f30 starting: java version 17.0.8 by Microsoft; OS Windows 11 arch amd64 version 10.0 [29jun.2024 18:08:14.178] [main/WARN] [net.minecraftforge.fml.loading.FMLConfig/CORE]: Configuration file C:\Users\Marco Antonio RL\AppData\Roaming\.minecraft\config\fml.toml is not correct. Correcting [29jun.2024 18:08:14.179] [main/INFO] [net.minecraftforge.fml.loading.FMLConfig/CORE]: Incorrect key [earlyWindowShowCPU] was corrected from null to false [29jun.2024 18:08:14.196] [main/INFO] [net.minecraftforge.fml.loading.ImmediateWindowHandler/]: Loading ImmediateWindowProvider fmlearlywindow [29jun.2024 18:08:14.341] [main/INFO] [EARLYDISPLAY/]: Trying GL version 4.6    
    • Hi, I was wondering how to make a "screenspace texture" like the background of the end portal (not including the moving stars). This is an example of what I want:  I have taken a look at the vanilla shader files, but I don't know how to use the effect for myself. Any help would be great,
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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