Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Libgdx cannot load texture issue

Tags:

java

libgdx

Cannot load any texture, it worked fine, now it doesn't work, don't know why.

import com.badlogic.gdx.graphics.Texture;

public class MiJuego implements ApplicationListener {
public Texture textura = new Texture(Gdx.files.internal("prueba.png"));

@Override
public void create() {
    // TODO Auto-generated method stub

}

Exception in thread "main" java.lang.NullPointerException at com.alex.version1.MiJuego.(MiJuego.java:16) at com.alex.version1.Main.main(Main.java:14)

Line 16 is exactly the one in which I create the texture. I have tested it in several projects but it doesn't work. The images are set in the asset folder in my android project.

like image 642
Alejandro Garcia Avatar asked May 23 '26 19:05

Alejandro Garcia


1 Answers

Try it like this:

public class MiJuego implements ApplicationListener {
public Texture textura;

@Override
public void create() {
    textura = new Texture(Gdx.files.internal("prueba.png"))
    ...
}

You were creating the Texture at the load time of the ApplicationListener, not create time. Libgdx isn't initialized then. So any Gdx.xxx call will throw a NullPointerException.

like image 155
Lestat Avatar answered May 26 '26 08:05

Lestat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!