문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top