NullPointerException when trying to createHeadlessWindow - Linux

Started by riz, August 17, 2010, 19:27:30

Previous topic - Next topic

riz

Apologies if this has been answered before, I have tried to look for solutions, but couldn't find anything in the forums. Me and a developer are developing a 3D game under Java. We use LWJGL library too. The problem we get is that when the application tries to determine the configuration of the user's system, i.e. the minimum screen resolution, bits, etc, a NullPointerException is being thrown.

This is being run under Linux Mint 9, 64-bit:

Linux riz-desktop 2.6.32-21-generic #32-Ubuntu SMP Fri Apr 16 08:09:38 UTC 2010 x86_64 GNU/Linux
The JDK is 1.6.0_20 and it's 32-bit and the LWJGL libraries being used are 32-bit too.
I am running NVIDIA graphics card 8800GT, with these settings:

NVIDIA Driver Version: 195.36.24
Server Version Number: 11.0
NV-CONTROL Version: 1.22
Screens: 1

Here is the code used:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package frames;

import com.jme.system.DisplaySystem;
import java.awt.DisplayMode;
import java.awt.GraphicsEnvironment;

/**
 *
 * @author Riz
 */
public class DisplaySetupDemo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (isMinimumScreenResolutionSupported()) {
            DisplaySystem display = DisplaySystem.getDisplaySystem();

            DisplayMode smallestDisplay = getMinimumAllowedDisplay();
            boolean fullScreen = false;
            int width = smallestDisplay.getWidth();
            int height = smallestDisplay.getHeight();
            int frequency = smallestDisplay.getRefreshRate();
            int colourDepth = 16;

            display.createHeadlessWindow( width, height, colourDepth );

            display.close();
        }
    }

    private static DisplayMode getMinimumAllowedDisplay()
    {
        DisplayMode[] displayModes = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayModes();
        System.out.println("Found " + displayModes.length + " display modes");
        DisplayMode minimum = null;

        for ( DisplayMode mode : displayModes )
        {
            System.out.println("Resolution found is " +
                        mode.getWidth() + "x" + mode.getHeight() + "@" +
                        mode.getRefreshRate() + "Hz, " + mode.getBitDepth() +
                        " bits");

            if ( minimum == null && aboveMinimumResolution( mode.getWidth(), mode.getHeight() ) )
            {
                minimum = mode;
                System.out.println("Minimum screen resolution supported is " +
                        minimum.getWidth() + "x" + minimum.getHeight() + "@" +
                        minimum.getRefreshRate() + "Hz, " + minimum.getBitDepth() +
                        " bits");
            }
        }

        // Should not have called getMinimumAllowedDisplay() unless already check user can support it
        assert minimum != null : "Users machine cannot support the game - should have check this with the static method";

        return minimum;
    }

    private static boolean aboveMinimumResolution( int width, int height )
    {
        return width >= 800 && height >= 600;
    }

    private static boolean isMinimumScreenResolutionSupported()
    {
        DisplayMode[] displayModes = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayModes();
        boolean supported = false;

        for ( DisplayMode mode : displayModes )
        {
            // If we haven't found an above minimum resolution mode yet
            if ( !supported )
            {
                // Is this mode above the minimum?
                supported = aboveMinimumResolution( mode.getWidth(), mode.getHeight() );
            }
        }
        System.out.println("Minimum screen resolution supported is " + supported);

        return supported;
    }
}


The exception I get is this:

Minimum screen resolution supported is true
17-Aug-2010 18:46:02 com.jme.system.lwjgl.LWJGLDisplaySystem <init>
INFO: LWJGL Display System created.
Found 115 display modes
Resolution found is 1920x1200@50Hz, -1 bits
Minimum screen resolution supported is 1920x1200@50Hz, -1 bits
Resolution found is 1920x1080@51Hz, -1 bits
Resolution found is 1920x1080@52Hz, -1 bits
Resolution found is 1920x1080@53Hz, -1 bits
Resolution found is 1920x1080@54Hz, -1 bits
Resolution found is 1680x1050@55Hz, -1 bits
Resolution found is 1680x1050@56Hz, -1 bits
Resolution found is 1680x1050@57Hz, -1 bits
Resolution found is 1680x1050@58Hz, -1 bits
Resolution found is 1600x1200@59Hz, -1 bits
Resolution found is 1600x1200@60Hz, -1 bits
Resolution found is 1600x1200@61Hz, -1 bits
Resolution found is 1600x1024@62Hz, -1 bits
Resolution found is 1440x900@63Hz, -1 bits
Resolution found is 1400x1050@64Hz, -1 bits
Resolution found is 1400x1050@65Hz, -1 bits
Resolution found is 1400x1050@66Hz, -1 bits
Resolution found is 1400x1050@67Hz, -1 bits
Resolution found is 1360x768@68Hz, -1 bits
Resolution found is 1360x768@69Hz, -1 bits
Resolution found is 1280x1024@70Hz, -1 bits
Resolution found is 1280x1024@71Hz, -1 bits
Resolution found is 1280x1024@72Hz, -1 bits
Resolution found is 1280x960@73Hz, -1 bits
Resolution found is 1280x960@74Hz, -1 bits
Resolution found is 1280x800@75Hz, -1 bits
Resolution found is 1280x720@76Hz, -1 bits
Resolution found is 1280x720@77Hz, -1 bits
Resolution found is 1152x864@78Hz, -1 bits
Resolution found is 1152x864@79Hz, -1 bits
Resolution found is 1152x864@80Hz, -1 bits
Resolution found is 1152x864@81Hz, -1 bits
Resolution found is 1152x864@82Hz, -1 bits
Resolution found is 1152x864@83Hz, -1 bits
Resolution found is 1152x864@84Hz, -1 bits
Resolution found is 1024x768@85Hz, -1 bits
Resolution found is 1024x768@86Hz, -1 bits
Resolution found is 1024x768@87Hz, -1 bits
Resolution found is 1024x768@88Hz, -1 bits
Resolution found is 1024x768@89Hz, -1 bits
Resolution found is 1024x768@90Hz, -1 bits
Resolution found is 1024x768@91Hz, -1 bits
Resolution found is 960x720@92Hz, -1 bits
Resolution found is 960x720@93Hz, -1 bits
Resolution found is 960x720@94Hz, -1 bits
Resolution found is 960x600@95Hz, -1 bits
Resolution found is 960x540@96Hz, -1 bits
Resolution found is 928x696@97Hz, -1 bits
Resolution found is 928x696@98Hz, -1 bits
Resolution found is 896x672@99Hz, -1 bits
Resolution found is 896x672@100Hz, -1 bits
Resolution found is 840x525@101Hz, -1 bits
Resolution found is 840x525@102Hz, -1 bits
Resolution found is 840x525@103Hz, -1 bits
Resolution found is 840x525@104Hz, -1 bits
Resolution found is 840x525@105Hz, -1 bits
Resolution found is 832x624@106Hz, -1 bits
Resolution found is 800x600@107Hz, -1 bits
Resolution found is 800x600@108Hz, -1 bits
Resolution found is 800x600@109Hz, -1 bits
Resolution found is 800x600@110Hz, -1 bits
Resolution found is 800x600@111Hz, -1 bits
Resolution found is 800x600@112Hz, -1 bits
Resolution found is 800x600@113Hz, -1 bits
Resolution found is 800x600@114Hz, -1 bits
Resolution found is 800x600@115Hz, -1 bits
Resolution found is 800x600@116Hz, -1 bits
Resolution found is 800x512@117Hz, -1 bits
Resolution found is 720x576@118Hz, -1 bits
Resolution found is 720x480@119Hz, -1 bits
Resolution found is 720x450@120Hz, -1 bits
Resolution found is 720x400@121Hz, -1 bits
Resolution found is 700x525@122Hz, -1 bits
Resolution found is 700x525@123Hz, -1 bits
Resolution found is 700x525@124Hz, -1 bits
Resolution found is 700x525@125Hz, -1 bits
Resolution found is 680x384@126Hz, -1 bits
Resolution found is 680x384@127Hz, -1 bits
Resolution found is 640x512@128Hz, -1 bits
Resolution found is 640x512@129Hz, -1 bits
Resolution found is 640x512@130Hz, -1 bits
Resolution found is 640x480@131Hz, -1 bits
Resolution found is 640x480@132Hz, -1 bits
Resolution found is 640x480@133Hz, -1 bits
Resolution found is 640x480@134Hz, -1 bits
Resolution found is 640x480@135Hz, -1 bits
Resolution found is 640x480@136Hz, -1 bits
Resolution found is 640x480@137Hz, -1 bits
Resolution found is 640x400@138Hz, -1 bits
Resolution found is 640x350@139Hz, -1 bits
Resolution found is 576x432@140Hz, -1 bits
Resolution found is 576x432@141Hz, -1 bits
Resolution found is 576x432@142Hz, -1 bits
Resolution found is 576x432@143Hz, -1 bits
Resolution found is 576x432@144Hz, -1 bits
Resolution found is 576x432@145Hz, -1 bits
Resolution found is 576x432@146Hz, -1 bits
Resolution found is 512x384@147Hz, -1 bits
Resolution found is 512x384@148Hz, -1 bits
Resolution found is 512x384@149Hz, -1 bits
Resolution found is 512x384@150Hz, -1 bits
Resolution found is 512x384@151Hz, -1 bits
Resolution found is 416x312@152Hz, -1 bits
Resolution found is 400x300@153Hz, -1 bits
Resolution found is 400x300@154Hz, -1 bits
Resolution found is 400x300@155Hz, -1 bits
Resolution found is 400x300@156Hz, -1 bits
Resolution found is 400x300@157Hz, -1 bits
Resolution found is 360x200@158Hz, -1 bits
Resolution found is 320x240@159Hz, -1 bits
Resolution found is 320x240@160Hz, -1 bits
Resolution found is 320x240@161Hz, -1 bits
Resolution found is 320x240@162Hz, -1 bits
Resolution found is 320x200@163Hz, -1 bits
Resolution found is 320x175@164Hz, -1 bits
17-Aug-2010 18:46:03 com.jme.system.lwjgl.LWJGLDisplaySystem initHeadlessDisplay
SEVERE: Cannot create headless window
17-Aug-2010 18:46:03 class com.jme.system.lwjgl.LWJGLDisplaySystem initHeadlessDisplay()
SEVERE: Exception
java.lang.NullPointerException: mode must be non-null
	at org.lwjgl.opengl.Display.setDisplayMode(Display.java:248)
	at com.jme.system.lwjgl.LWJGLDisplaySystem.initHeadlessDisplay(LWJGLDisplaySystem.java:478)
	at com.jme.system.lwjgl.LWJGLDisplaySystem.createHeadlessWindow(LWJGLDisplaySystem.java:173)
	at frames.DisplaySetupDemo.main(DisplaySetupDemo.java:32)
Exception in thread "main" java.lang.Error: Cannot create headless window: mode must be non-null
	at com.jme.system.lwjgl.LWJGLDisplaySystem.initHeadlessDisplay(LWJGLDisplaySystem.java:491)
	at com.jme.system.lwjgl.LWJGLDisplaySystem.createHeadlessWindow(LWJGLDisplaySystem.java:173)
	at frames.DisplaySetupDemo.main(DisplaySetupDemo.java:32)
Caused by: java.lang.NullPointerException: mode must be non-null
	at org.lwjgl.opengl.Display.setDisplayMode(Display.java:248)
	at com.jme.system.lwjgl.LWJGLDisplaySystem.initHeadlessDisplay(LWJGLDisplaySystem.java:478)
	... 2 more


This code works under Windows, without any problems, but fails with the exception above under Linux.
If I create a normal window, not a headless window (using different code), this works under Linux and Windows. I can provide code for that, it was from forum thread on these forums at request.

Any ideas?

Thanks again and apologies if I missed anything.

Riz

Matthias

Why do you use AWT code if you want to check for display modes supported by LWJGL?
Also consider running in a windowed mode - this should work even if you can't find the desired full screen display mode.

riz

I use AWT code to get the bits, refresh rate, etc. Maybe that could be why the bits always come back as -1 regardless of the mode.

I will try it using the org.lwjgl classes and see what happens. I know that the window mode works because I can run window mode under linux and this is fine.

Thanks again Matthias, I will report the results.

riz

I have modified the code and now have removed the AWT code and use purely LWJGL code. Here is the code now:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package frames;

import com.jme.system.DisplaySystem;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.DisplayMode;

/**
 *
 * @author Riz
 */
public class DisplaySetupDemo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws LWJGLException {
        if (isMinimumScreenResolutionSupported()) {
            DisplaySystem display = DisplaySystem.getDisplaySystem();

            DisplayMode smallestDisplay = getMinimumAllowedDisplay();
            boolean fullScreen = false;
            int width = smallestDisplay.getWidth();
            int height = smallestDisplay.getHeight();
            int frequency = smallestDisplay.getFrequency();
            int colourDepth = 16;

            display.createHeadlessWindow( width, height, colourDepth );

            display.close();
        }
    }

    private static DisplayMode getMinimumAllowedDisplay() throws LWJGLException
    {
        DisplayMode[] displayModes = org.lwjgl.util.Display.getAvailableDisplayModes(320, 240, -1, -1, -1, -1, 60, 85);
        System.out.println("Found " + displayModes.length + " display modes");
        DisplayMode minimum = null;

        for ( DisplayMode mode : displayModes )
        {
            System.out.println("Resolution found is " +
                        mode.getWidth() + "x" + mode.getHeight() + "@" +
                        mode.getFrequency() + "Hz, " + mode.getBitsPerPixel() +
                        " bits");

            if ( minimum == null && aboveMinimumResolution( mode.getWidth(), mode.getHeight() ) )
            {
                minimum = mode;
                System.out.println("Minimum screen resolution supported is " +
                        minimum.getWidth() + "x" + minimum.getHeight() + "@" +
                        minimum.getFrequency() + "Hz, " + minimum.getBitsPerPixel() +
                        " bits");
            }
        }

        // Should not have called getMinimumAllowedDisplay() unless already check user can support it
        assert minimum != null : "Users machine cannot support the game - should have check this with the static method";

        return minimum;
    }

    private static boolean aboveMinimumResolution( int width, int height )
    {
        return width >= 800 && height >= 600;
    }

    private static boolean isMinimumScreenResolutionSupported() throws LWJGLException
    {
        DisplayMode[] displayModes = org.lwjgl.util.Display.getAvailableDisplayModes(320, 240, -1, -1, -1, -1, 60, 85);
        boolean supported = false;

        for ( DisplayMode mode : displayModes )
        {
            // If we haven't found an above minimum resolution mode yet
            if ( !supported )
            {
                // Is this mode above the minimum?
                supported = aboveMinimumResolution( mode.getWidth(), mode.getHeight() );
            }
        }
        System.out.println("Minimum screen resolution supported is " + supported);

        return supported;
    }
}


When I run this, I still get the NPE exception that says mode must be non-null. What am I doing wrong or not doing?

Minimum screen resolution supported is true
20-Aug-2010 17:58:06 com.jme.system.lwjgl.LWJGLDisplaySystem <init>
INFO: LWJGL Display System created.
Found 26 display modes
Resolution found is 1280x800@75Hz, 24 bits
Minimum screen resolution supported is 1280x800@75Hz, 24 bits
Resolution found is 1400x1050@67Hz, 24 bits
Resolution found is 1400x1050@66Hz, 24 bits
Resolution found is 1400x1050@65Hz, 24 bits
Resolution found is 1400x1050@64Hz, 24 bits
Resolution found is 1024x768@85Hz, 24 bits
Resolution found is 1600x1024@62Hz, 24 bits
Resolution found is 1280x1024@72Hz, 24 bits
Resolution found is 1280x1024@71Hz, 24 bits
Resolution found is 1280x1024@70Hz, 24 bits
Resolution found is 1360x768@68Hz, 24 bits
Resolution found is 1360x768@69Hz, 24 bits
Resolution found is 1440x900@63Hz, 24 bits
Resolution found is 1152x864@78Hz, 24 bits
Resolution found is 1152x864@79Hz, 24 bits
Resolution found is 1152x864@84Hz, 24 bits
Resolution found is 1152x864@81Hz, 24 bits
Resolution found is 1152x864@80Hz, 24 bits
Resolution found is 1152x864@83Hz, 24 bits
Resolution found is 1152x864@82Hz, 24 bits
Resolution found is 1280x720@77Hz, 24 bits
Resolution found is 1280x720@76Hz, 24 bits
Resolution found is 1280x960@73Hz, 24 bits
Resolution found is 1280x960@74Hz, 24 bits
Resolution found is 1600x1200@60Hz, 24 bits
Resolution found is 1600x1200@61Hz, 24 bits
20-Aug-2010 17:58:06 com.jme.system.lwjgl.LWJGLDisplaySystem initHeadlessDisplay
SEVERE: Cannot create headless window
20-Aug-2010 17:58:06 class com.jme.system.lwjgl.LWJGLDisplaySystem initHeadlessDisplay()
SEVERE: Exception
java.lang.NullPointerException: mode must be non-null
	at org.lwjgl.opengl.Display.setDisplayMode(Display.java:248)
	at com.jme.system.lwjgl.LWJGLDisplaySystem.initHeadlessDisplay(LWJGLDisplaySystem.java:478)
	at com.jme.system.lwjgl.LWJGLDisplaySystem.createHeadlessWindow(LWJGLDisplaySystem.java:173)
	at frames.DisplaySetupDemo.main(DisplaySetupDemo.java:32)
Exception in thread "main" java.lang.Error: Cannot create headless window: mode must be non-null
	at com.jme.system.lwjgl.LWJGLDisplaySystem.initHeadlessDisplay(LWJGLDisplaySystem.java:491)
	at com.jme.system.lwjgl.LWJGLDisplaySystem.createHeadlessWindow(LWJGLDisplaySystem.java:173)
	at frames.DisplaySetupDemo.main(DisplaySetupDemo.java:32)
Caused by: java.lang.NullPointerException: mode must be non-null
	at org.lwjgl.opengl.Display.setDisplayMode(Display.java:248)
	at com.jme.system.lwjgl.LWJGLDisplaySystem.initHeadlessDisplay(LWJGLDisplaySystem.java:478)
	... 2 more


Thanks again
Riz