S
S
Sergey Semenko2015-04-03 15:40:39
Java
Sergey Semenko, 2015-04-03 15:40:39

Why doesn't the following code (libGDX) work?

There is the following code

map = new Map(this);
        map.setPosition(0, 0);
        map.setLocations(locations);
        stage.addActor(map);

public void setLocations(ArrayList<Location> locations) {
        for (int i = 0; i < locations.size(); i++) {
            Location location = locations.get(i);
            LocationSelector locationSelector = new LocationSelector(this, location);
            getStage().addActor(locationSelector);
        }
    }

public class LocationSelector extends Actor {
    public LocationSelector(Map map, Location location) {
        setSize(location.WIDTH * map.ratioX, location.HEIGHT * map.ratioY);
        setPosition(location.x * map.ratioX, location.y * map.ratioY);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        batch.draw(Assets.location, getX(), getY(), getWidth(), getHeight());
    }
}

abstract class Location {
    public static final float WIDTH = 60;
    public static final float HEIGHT = 60;
    public float x, y;
}

I see this error in the logs
04-03 15:32:43.106  24656-24672/ru.redozote.mrr2.android E/AndroidRuntime﹕ FATAL EXCEPTION: GLThread 49532
    java.lang.NullPointerException
            at ru.redozote.mrr2.Actors.Map.setLocations(Map.java:38)
            at ru.redozote.mrr2.ChooseLocationScreen.<init>(ChooseLocationScreen.java:39)
            at ru.redozote.mrr2.Actors.PlayButton$1.clicked(PlayButton.java:29)
            at com.badlogic.gdx.scenes.scene2d.utils.ClickListener.touchUp(ClickListener.java:89)
            at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:57)
            at com.badlogic.gdx.scenes.scene2d.Stage.touchUp(Stage.java:348)
            at com.badlogic.gdx.backends.android.AndroidInput.processEvents(AndroidInput.java:360)
            at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:420)
            at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1516)
            at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question