Gdx.graphics NPE?

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

Gdx.graphics NPE?

Postby nemewsys » Tue Apr 29, 2014 2:32 pm

I'm getting a null pointer exception while trying to access Gdx.graphics.
How should I get the screen resolution in order to make the window with that same resolution?

Code: Select all
package com.mygdx.game.desktop;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.mygdx.game.MyGdxGame;

public class DesktopLauncher {
   public static void main(String[] arg) {
      LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
      /**
       * Get screen resolution
       */
      config.width = Gdx.graphics.getWidth();
      config.height = Gdx.graphics.getWidth();
      config.fullscreen = true;
      new LwjglApplication(new MyGdxGame(), config);
   }
}


Thanks!
nemewsys
 
Posts: 3
Joined: Tue Apr 29, 2014 2:30 pm

Re: Gdx.graphics NPE?

Postby BurningHand » Tue Apr 29, 2014 5:39 pm

The Gdx.* static fields are not valid until the Application has been initialized (not until your create() method has been called).

You can get the resolution by calling LwjglApplicationConfiguration.getDesktopDisplayMode() or to get a list of supported display modes call LwjglApplicationConfiguration.getDisplayModes().

For your case, doing config.setFromDisplayMode(LwjglApplicationConfiguration.getDesktopDisplayMode()); is probably sufficient.
IRC: nexsoftware / mobidevelop; GitHub: MobiDevelop;
BurningHand
 
Posts: 2812
Joined: Mon Oct 25, 2010 4:35 am

Re: Gdx.graphics NPE?

Postby nemewsys » Tue Apr 29, 2014 6:13 pm

Thanks!
nemewsys
 
Posts: 3
Joined: Tue Apr 29, 2014 2:30 pm

Re: Gdx.graphics NPE?

Postby Nagi » Sat Jul 26, 2014 5:23 pm

The wonders of a seemingly inoffensive post... BurningHand's reply contains the answer for a long standing problem I was facing: how to get a list of supported display modes before creating the Application. Thank you!
Programmer's apprentice at IKIGames - @superikigames
Nagi
 
Posts: 13
Joined: Mon Dec 03, 2012 10:53 am
Location: Deep in the far away Caribbean


Return to General Development

Who is online

Users browsing this forum: No registered users and 3 guests