Null Pointer Exception

Anything libgdx related goes here!

Null Pointer Exception

Postby Person X » Fri Aug 16, 2019 1:45 pm

When I try to create a skin on the main menu, I get a null pointer exception, though graphicsSkin contains the regions of graphicsAtlas.

The code, the error line is marked:

import...

public class MainMenuScreen implements Screen {

private SpriteBatch batch;
protected Stage stage;
private Viewport viewport;
private OrthographicCamera camera;
private TextureAtlas graphicsAtlas;
protected Skin graphicsSkin;
int width;
int height;
Skin buttonSkin;

public MainMenuScreen() {
width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();
batch = new SpriteBatch();
camera = new OrthographicCamera(width, height);
viewport = new FillViewport(width, height, camera);
viewport.apply();
AssetsManager manager = new AssetsManager();
SkinParameter parameters = new SkinParameter("Background Image.atlas");
manager.loadAtlas("MainMenuGraphics.atlas");
manager.manager.finishLoading();
graphicsAtlas = manager.manager.get("MainMenuGraphics.atlas");
graphicsSkin.addRegions(graphicsAtlas); //THE ERROR OCCURS HERE (THIS IS LINE 46)//
buttonSkin = graphicsSkin.get("buttonSkin", Skin.class);
stage = new Stage(viewport, batch);
}
@Override
public void show() {
Gdx.input.setInputProcessor(stage);
Table mainTable = new Table();
mainTable.setFillParent(true);
mainTable.top();
TextButton campaignButton = new TextButton("Campaign", buttonSkin);
TextButton onlineButton = new TextButton("Online", buttonSkin);
TextButton exitButton = new TextButton("Exit Game", buttonSkin);

campaignButton.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
((Game)Gdx.app.getApplicationListener()).setScreen(new
CampaignScreen());
}
});
exitButton.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.exit();
}
});
mainTable.add(campaignButton);
mainTable.row();
mainTable.add(onlineButton);
mainTable.row();
mainTable.add(exitButton);
stage.addActor(mainTable);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(.1f, .12f, .16f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
@Override
public void resize(int width, int height) {
viewport.update(width, height);
camera.position.set(camera.viewportWidth / 2,
camera.viewportHeight / 2, 0);
camera.update();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
}
}

The error message:

WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by org.lwjgl.LWJGLUtil$3 (file:/C:/Users/perso/.gradle/caches/modules-2/files-2.1/org.lwjgl.lwjgl/lwjgl/2.9.3/3df168ac74e4a8c96562cdff24ad352e255bf89c/lwjgl-2.9.3.jar) to method java.lang.ClassLoader.findLibrary(java.lang.String) WARNING: Please consider reporting this to the maintainers of org.lwjgl.LWJGLUtil$3 WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release Exception in thread "LWJGL Application" java.lang.NullPointerException at com.project1.game.MainMenuScreen.(MainMenuScreen.java:46)
Person X
 
Posts: 3
Joined: Fri Aug 16, 2019 1:39 pm

Re: Null Pointer Exception

Postby shatterblast » Fri Aug 16, 2019 6:56 pm

You can wrap your code in "[code][/code]". There is also the Code button above if you can see it in the editor.

My first suggestion involves using underscores ( _ ) in your file names instead of spaces.

"AssetsManager" to my knowledge does not exist. It should be AssetManager. How did that even compile? That should not have made it far enough to produce an NPE at all.
shatterblast
 
Posts: 414
Joined: Sun Jul 06, 2014 1:14 pm

Re: Null Pointer Exception

Postby Person X » Fri Aug 16, 2019 9:40 pm

Actually AssetsManager is a class I created to easily reference asset manager, as I need only pass the internal file path as parameters to do so. Sorry, I should have been more specific and posted that code as well, I completely forgot. I don't think the error is caused by spaces in the file names, though, as I have successfully loaded other files with spaces in their names before.
Person X
 
Posts: 3
Joined: Fri Aug 16, 2019 1:39 pm

Re: Null Pointer Exception

Postby shatterblast » Fri Aug 16, 2019 9:58 pm

In my code, where ...

getManager() points to AssetManager and
skin is Skin skin;


I use something like the following:
Code: Select all
getManager().load( "skin.json", Skin.class );
skin = getManager().get( "skin.json", Skin.class );



In your code, this might translate to:
Code: Select all
manager.load( "skin.json", Skin.class );
manager.manager.finishLoading();
graphicsSkin = manager.get( "skin.json", Skin.class );
buttonSkin = graphicsSkin;


Your skin files should have a JSON file in there somewhere.
shatterblast
 
Posts: 414
Joined: Sun Jul 06, 2014 1:14 pm

Re: Null Pointer Exception

Postby tenfour04 » Sat Aug 17, 2019 1:16 am

I think you have a mix-up with your skins. Why are there two skin variables? The skin is a collection of styles for all types of UI components. You should only have one. This code
Code: Select all
buttonSkin = graphicsSkin.get("buttonSkin", Skin.class);
doesn't make sense.

You can pass your single skin to the TextButton constructors and they will automatically extract the TextButtonStyle named "default".

Your NPE is because you're calling a method on graphicsSkin before instantiating a Skin and assigning it to graphicsSkin. You would fix it by adding this line above it:
Code: Select all
graphicsSkin = new Skin();
But a Skin still needs a JSON file to define all its styles, or you otherwise would need to define them programmatically, which you haven't done here.

Typically, you would simply load the Skin with your asset manager directly by passing in the JSON file. It would automatically determine the atlas, so you wouldn't have to mess with TextureAtlas at all.
tenfour04
 
Posts: 1233
Joined: Sat Jun 18, 2011 3:24 pm

Re: Null Pointer Exception

Postby Person X » Sat Aug 17, 2019 8:10 am

Thanks for the help, shatterblast and tenfour04, I now understand where I went wrong as this is my first time dealing with this sort of thing, and the online pages are of very little help as each one says something almost completely different. I don't actually have a json file, I'm not really sure what this means, but I didn't select html in the project creation page if that helps. Also, tenfour04, you said something about a 'default' skin for text buttons, is this something I manually set or is it generated within the atlas?
Person X
 
Posts: 3
Joined: Fri Aug 16, 2019 1:39 pm


Return to Libgdx

Who is online

Users browsing this forum: Google [Bot], MSN [Bot] and 1 guest