Jump to content

[SOLVED][1.13.2]I wanted to create a GUI Button but it gave me the null pointer error


Recommended Posts

Posted (edited)
@Override
protected void initGui() {
    this.buttons.clear();
    this.buttons.add(this.buttonOk = new GuiButton(0, this.width / 2, this.height - buttonOk.height / 2,"Ok")
    {
        @Override
        public void onClick(double p_194829_1_, double p_194829_3_) {
            super.onClick(p_194829_1_, p_194829_3_);
        }
    });
    super.initGui();
}

This code is in my custom Gui which is a subclass of GuiScreen

 

This error happens when I entered my world

  Reveal hidden contents

I was wondering why declaring a new GuiButton instance give me a null pointer error

Edited by yuckyh
Putting bunch of code/log to a spoiler to improve readibility
Posted (edited)

1. Use the spoiler to post big bunch of code/log.

2. 

  On 3/12/2019 at 1:30 PM, yuckyh said:

at com.yuckyh.equisora.client.gui.GuiClassSelect.initGui(GuiClassSelect.java:28) ~[classes/:?]

Expand  

Something at line 28 is null. Figure it out.

 

Hint:

  Reveal hidden contents

 

Edited by DavidM

Some tips:

  Reveal hidden contents

 

Posted
 
  Reveal hidden contents

The null pointer exception is gone, but my button never appeared in the gui screen

Posted
  On 3/12/2019 at 3:12 PM, diesieben07 said:

You must first call super.initGui before adding your buttons.

Expand  
package com.yuckyh.equisora.client.gui;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;

public class GuiClassSelect extends GuiScreen
{
    private int guiWidth = 800;
    private int guiHeight = 450;
    private int guiCenterX = (this.width/2) - guiWidth/2;
    private int guiCenterY = (this.height/2) - guiHeight/2;

    @Override
    public void render(int mouseX, int mouseY, float partialTicks)
    {
        super.render(mouseX, mouseY, partialTicks);
    }

    @Override
    protected void initGui() {
        super.initGui();
        GuiButton buttonOk = new GuiButton(0, this.width / 2 - width, this.height * 5 / 6, "Ok") {
            @Override
            public void onClick(double p_194829_1_, double p_194829_3_) {
                super.onClick(p_194829_1_, p_194829_3_);
            }
        };
        this.buttons.add(buttonOk);
    }

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

Is it like that? if it is, it didn't work

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.