I get a strange Nullpointer exception and I don't understand why. Can someone help me ?


Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.badlogic.gdx.scenes.scene2d.ui.ScrollPane.draw(ScrollPane.java:583)
    at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:123)
    at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:57)
    at com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.draw(WidgetGroup.java:163)
    at com.badlogic.gdx.scenes.scene2d.ui.Table.draw(Table.java:119)
    at com.solvapps.tests.NewStoryBook.draw(NewStoryBook.java:40)

My Code ACTOR:

public class NewStoryBook extends Actor {

    Table table;
    ScrollPane pane;
    Table tableScroller;

    public NewStoryBook(Skin skin) {

        int w = Gdx.graphics.getWidth();
        int h = Gdx.graphics.getHeight();
        table = new Table();
        table.setPosition(400, 400);
        table.setSize(w / 3, h * 0.8f);
        tableScroller = new Table();
        tableScroller.add(new Label("bla bla",skin));
        pane = new ScrollPane(tableScroller);



    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);
        table.draw(batch, parentAlpha);

My Code App:

public class StoryBoxTest4 extends ApplicationAdapter {

    private Stage stage;
    NewStoryBook newstoryBook;

    public void create() {
        stage = new Stage(new ScreenViewport());
        Skin skin = new Skin(Gdx.files.internal(SkinManager.getSkin()));
        newstoryBook = new NewStoryBook(skin);

This line is the line that throws a NullPointerException:

getStage().calculateScissors(widgetAreaBounds, scissorBounds);

From this we can deduce that getStage() is returning null. Why? Because you haven't added your table to a Stage. Instead you draw it manually in your NewStoryBook actor which is something you shouldn't do.

Solution? Make NewStoryBook extend Group instead and use addActor:

public class NewStoryBook extends Group {

    public NewStoryBook(Skin skin) {

    // Remove draw() function
  • \$\begingroup\$ Thank you so much. You did not only answered my question, you solved my general design problem. \$\endgroup\$ Nov 16 '18 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.