0
votes

I am trying to get LibGDX to take a screen shot of the devices screen and then load that screenshot into a Sprite to render later. However when I try it the app crashes with a NullPointerException. I have tried the following links: taking screenshot in libgdx, and using ScreenUtils to save screenshot as image in libgdx. When I try to create the Texture for the Sprite then it crashes with NullPointerException.

I have also tried the ScreenUtils.getFrameBufferTexture(); method, and the ScreenUtils.getFrameBufferPixmap() method but always they crash with a NullPointerException. The line they crash on changes depending upon the way I have chosen to get the screen shot, and I have confirmed that indeed the Pixmap or TextureRegion is created when the screenshot is taken. It is when I try to either load it into a Texture or the Sprite that it crashes with a NullPointerException.

PLEASE DO NOT mark this as a duplicate question as I am not trying to save the screenshot into a PNG. I am trying to use it directly in a Sprite and it is crashing.

Code and stack trace for creating a TextureRegion:

// create the screen shot
UT.screenShot = ScreenUtils.getFrameBufferTexture(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());


// Use screenshot
screenShot = new Sprite(UT.screenShot);

I get this stack trace:

Exception in thread "LWJGL Application" java.lang.NullPointerException

at com.badlogic.gdx.graphics.g2d.TextureRegion.setRegion(TextureRegion.java:112)
at com.badlogic.gdx.graphics.g2d.Sprite.<init>(Sprite.java:83)
at *** MY CODE *** CompleteScreen.java:83
at *** MY CODE *** GameEngine.java:82
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

This is only one of the ways I have used to create the screenshot. But all the ways use the ScreenUtils class to get the screenshot. All these ways do indeed create a screenshot. However all these ways fail when trying to create a Sprite or Texture.

Code and stack trace for creating a Pixmap:

// create the screen shot
UT.screenShot = ScreenUtils.getFrameBufferPixmap(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());


// Use screenshot
Texture texture = new Texture(UT.screenShot);
screenShot = new Sprite(texture);

I get this stack trace:

Exception in thread "LWJGL Application" java.lang.NullPointerException

at com.badlogic.gdx.graphics.glutils.PixmapTextureData.<init>(PixmapTextureData.java:37)
at com.badlogic.gdx.graphics.glutils.PixmapTextureData.<init>(PixmapTextureData.java:32)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:104)
at *** MY CODE *** CompleteScreen.java:84
at *** MY CODE *** GameEngine.java:82
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
1
Why don't you post the stack trace of either method as well as your code? Without having any informations about the Exception as well as your code we cannot help.Robert P

1 Answers

0
votes

So I have solved this problem only to create another problem. Oh the joys of programming! ;-) Anyway I will ask about my second problem in another question. The answer to this question is that I was getting rid of the Pixmap or TextureRegion too soon.

What kind of code do you get at the end of programming for 12 hours straight? Bone headed code!

Oh well lesson learned, only program for 11 hours. ;-)