Question

I want to start a libgdx game using preferences saved from the options menu in the game when it is started back up. But whenever i try to instantiate a preferences file, i get a null pointer exception?

Exception in thread "main" java.lang.NullPointerException
    at exampleGame.Main.main(Main.java:11)

Line 11 is Preferences prefs = Gdx.app.getPreferences("game.prefs"); i've also tried using a class that i made that does the exact same thing but just from the class.

The line would be PreferencesLoader prefs = new PreferencesLoader(); and inside that class is Preferences prefs = Gdx.app.getPreferences("game.prefs");

So how do i instantiate Preferences prefs = Gdx.app.getPreferences("game.prefs"); correctly so i can load preferences?

Was it helpful?

Solution

This is correct:

Preferences prefs = Gdx.app.getPreferences("game.prefs");

But you are trying to do it before the library is initialized. At least thats what I can deduce being its just the line 11 in the Main java class.

Exception in thread "main" java.lang.NullPointerException at exampleGame.Main.main(Main.java:11)

All the Gdx.xxx will be null before the initialize is complete.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top