0

I'm new to LibGDX and I am taking it slowly. When I run the application it just force closes due to an error. But I'm not sure what the problem is. Here is the code for the button that I have.

@Override
public void show() {
 // Viewport Camera
 camera = new PerspectiveCamera();
 viewport = new ExtendViewport(800, 480, camera);
 stage = new Stage(new ExtendViewport(800, 840));
 Gdx.input.setInputProcessor(stage);
 skin = new Skin(atlas);
 atlas = new TextureAtlas(Gdx.files.internal("ui/button.pack"));
 table = new Table(skin);
 table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
 textButtonStyle = new TextButtonStyle();
 textButtonStyle.up = skin.getDrawable("button.up");
 textButtonStyle.down = skin.getDrawable("button.down");
 buttonPlay = new TextButton("Play", textButtonStyle);
 buttonPlay.setWidth((float) (Gdx.graphics.getWidth()/2.5));
 buttonPlay.setHeight(Gdx.graphics.getHeight()/6);
 buttonPlay.setPosition((Gdx.graphics.getWidth()/2-buttonPlay.getWidth()/2), (float) (Gdx.graphics.getHeight()-(buttonPlay.getHeight())*2.5));
 buttonPlay.toFront();
 stage.addActor(buttonPlay);
 table.debug();
 table.add(buttonPlay);
}

The error:

Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.scenes.scene2d.ui.Skin.addRegions(Skin.java:102)
at com.badlogic.gdx.scenes.scene2d.ui.Skin.<init>(Skin.java:88)
at com.badlogic.gdx.screens.Menu.show(Menu.java:83)
at com.badlogic.gdx.Game.setScreen(Game.java:61)
at com.badlogic.gdx.screens.Splash.render(Splash.java:38)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.dakotapederson.slingshotsteve.SlingshotSteve.render(SlingshotSteve.java:29)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication1ドル.run(LwjglApplication.java:114)

Line 75 is where the show() method is.

asked Sep 13, 2014 at 19:30
2
  • What is the error? Provide log please. Commented Sep 13, 2014 at 19:48
  • Their you go, I update the code with a table to see if that would fix things Commented Sep 13, 2014 at 19:54

1 Answer 1

1

Your problem is that you are initialising your skin with an atlas before your atlas has been initialised, the atlas is the null pointer. You have done:

skin = new Skin(atlas); //Here atlas isn't loaded so exception occurs
atlas = new TextureAtlas(Gdx.files.internal("ui/button.pack"));

When it should be:

atlas = new TextureAtlas(Gdx.files.internal("ui/button.pack")); // Load atlas first
skin = new Skin(atlas);
answered Sep 13, 2014 at 19:58
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it fixed that problem, but then I get a "Missing LabelStyleFont Error". What do I do about that?
The problem you are getting is probably because your uiskin atlas doesn't have a style for the label you are making, the one your are loading from "ui/button.pack" sounds like it only has a UI for a button. You can read more about UI skins at:github.com/libgdx/libgdx/wiki/Skin. If you want a full pre-made UI atlas you can download one here: stackoverflow.com/questions/16182844/default-skin-libgdx

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.