nullpointerexception lwjgl.opengl.GL11.glClear(G11.java:576)

Started by hedphuq, March 03, 2006, 10:06:12

Previous topic - Next topic

hedphuq

I've just started out with lwjgl using this tutorial but I keep getting a nullpointerexception in the first example.

Quote
java.lang.NullPointerException
       at org.lwjgl.opengl.GL11.glClear(GL11.java:576)
       at platformgame.Main.render(Main.java:53)
       at platformgame.Main.mainLoop(Main.java:42)
       at platformgame.Main.main(Main.java:67)


I don't understand why I keep getting this, because I use Netbeans and I have added the libraries to my project libraries (wich is supposed to add them to the classpath at compile-time). Anyone has a clue?

Here's the code, you never know it's usefull:
/*
 * Main.java
 *
 * Created on 23 februari 2006, 19:52
 */

package platformgame;
import bsh.*;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.LWJGLException;
import java.io.*;


/**
 *
 * @author Ruben
 */
public class Main{
    
    public static DisplayMode findDisplayMode(int width, int height, int bitDepth) throws LWJGLException{
        DisplayMode[] modes = Display.getAvailableDisplayModes();
        for (int i = 0; i < modes.length; i++) {
            if (modes[i].getWidth() == width && modes[i].getHeight() == height &&
                    modes[i].getBitsPerPixel() >= bitDepth && modes[i].getFrequency() <= 85) {
                try {
                    Display.setDisplayMode(modes[i]);
                } catch (LWJGLException e) {
                    e.printStackTrace();
                }
                return modes[i];
            }
        }
        return null;
    }
    
    public static void mainLoop() throws Exception {
        int frameCount = 0;
        while( true ) {
            render();
            Display.update();
            if ( frameCount > 10000 ) {
                break;
            }
            frameCount++;
        }
        cleanup();
    }
    
    public static void render() {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); // clear the screen
        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // set the clear color to black
    }
    
    public static void cleanup() {
        Display.destroy();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            findDisplayMode(600, 800, 8);
            mainLoop();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

hedphuq

I've tried changing java.library.path, but I don't seem to have any succes, I keep getting UnsatisfiedLinkError.
I used this in commandline:
Quotejava -jar "C:\Documents and Settings\Ruben\java\PlatformGame\dist\PlatformGame.jar" -Djava.library.path="C:\Documents and Settings\Ruben\java\lwjgl\native\lwjgl.dll"

I used commandline because Netbeans didn't state the errors clear

Odeamus

I don't see a call to Display.create() anywhere. You need to call it before doing any opengl calls.

O.

hedphuq

I've added Display.create() now, but it doesn't fix the problem, have I added it at the right place?

/*
 * JXmlGui.java
 *
 * Created on 16 december 2005, 19:20
 */

package jxmlgui;

import bsh.EvalError;
import bsh.TargetError;
import java.io.IOException;
import javax.swing.JFrame;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 *
 * @author Ruben
 */
public class JXmlGui {
    static XMLReader producer;
    static Handler consumer;
    
    static {
        try {
            //get an instance of the default XML parser class
            producer = XMLReaderFactory.createXMLReader();
        } catch(SAXException e) {
            error(e);
        }
        
        try {
            //get a consumer for the parser events
            consumer = new Handler();
            //set the contenthandler
            producer.setContentHandler(consumer);
            //set the errorhandler
            producer.setErrorHandler(consumer);
        } catch(Exception e) {
            //the consumer setup can produce errors
            error(e);
        }
    }
    
    /** parsing method */
    public static void parseFile(String file) {
        try {
            //parse the file
            producer.parse(file);
            //error when reading of file doesn't succeed
        } catch(IOException e) {
            error(e);
            //error from sax
        } catch(SAXException e) {
            error(e);
        }
    }
    
    /** default method for handling errors (needs work as you can see)*/
    public static void error(Exception e) {
        e.printStackTrace();
    }
    
    static Object init(String kind, Attributes atts) {
        String bla = new String("bla");
        return bla;
    }
    
    public static void main(String[] args) {
        System.out.println("loading..."); //just print something to see if it's actually working
        try {
            parseFile("gui.xml"); //parse the file
        } catch(Exception e) {
            error(e); //catch errors
        }
        //just some test for the beanshell interpreter
        try {
            new bsh.Interpreter().eval("print(\"bsh works\")");
        } catch ( TargetError e ) {
            System.out.println("The script or code called by the script threw an exception: " + e.getTarget() );
        } catch ( EvalError e2 )    {
            System.out.println("There was an error in evaluating the script:" + e2 );
        }
    }
}


It compiles without problems, but when I run it Netbeans says:
Quotejava.lang.NoClassDefFoundError: and
Exception in thread "main"
Java Result: 1

Odeamus

All I can say is that you pasted the wrong code?

O.

hedphuq

oops, I guess I opened the wrong project  :oops:

/*
 * Main.java
 *
 * Created on 23 februari 2006, 19:52
 */

package platformgame;
import bsh.*;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.LWJGLException;
import java.io.*;


/**
 *
 * @author Ruben
 */
public class Main{
    
    public static DisplayMode findDisplayMode(int width, int height, int bitDepth) throws LWJGLException{
        DisplayMode[] modes = Display.getAvailableDisplayModes();
        for (int i = 0; i < modes.length; i++) {
            if (modes[i].getWidth() == width && modes[i].getHeight() == height &&
                    modes[i].getBitsPerPixel() >= bitDepth && modes[i].getFrequency() <= 85) {
                try {
                    Display.setDisplayMode(modes[i]);
                } catch (LWJGLException e) {
                    e.printStackTrace();
                }
                return modes[i];
            }
        }
        return null;
    }
    
    public static void mainLoop() throws Exception {
        int frameCount = 0;
        while( true ) {
            render();
            Display.update();
            if ( frameCount > 10000 ) {
                break;
            }
            frameCount++;
        }
        cleanup();
    }
    
    public static void render() {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); // clear the screen
        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // set the clear color to black
    }
    
    public static void cleanup() {
        Display.destroy();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            Display.create();
        } catch (LWJGLException ex) {
            ex.printStackTrace();
        }
        try {
            findDisplayMode(600, 800, 8);
            mainLoop();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}


the error was the right one

Odeamus

Hmm... All I can say is that it seems to be a problem with your launch configuration. It worked for me. All I found was couple unused imports, but that's all.
You might want to change the
if ( frameCount > 10000 ) {
                break;
}

part to
if (Display.isCloseRequested()) {
                break;
}


And add a Thread.yield() there too at the end of the loop. Much better that way.

hedphuq

did you compile it with netbeans IDE (5.0)?
Anyway, could you show me the VM options you used to run it?

Odeamus

Yes, I did.

Go to project properties and there in the Libraries tab add the lwjgl jars to the compile list. Then at the Run tab add -Djava.library.path=<path to native dir> to the VM options field.

That's all I did.

I hope that helps.

O.

Fool Running

QuoteIt compiles without problems, but when I run it Netbeans says:
java.lang.NoClassDefFoundError: and
Exception in thread "main"
Java Result: 1
It looks like you might have set up the project to try run starting with a class called "and." Try looking at your project settings.
Right-click on the project and go to properties. Look under the run item for the main class.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

hedphuq

I checked, but it I've set the right main class in the properties.
I started a new project, copied the source-code, and deleted the old project, but the problem remains. Could this be a Netbeans bug?

The only "and" I can think of, is in the path to the lwjgl libraries.
Could it be something goes wrong there? It seems unlikely to me.

Ofcourse, there was a second "and", in this line:
Quote-Djava.library.path=C:\Documents and Settings\Ruben\java\lwjgl\native
fixed with:
Quote-Djava.library.path="C:\Documents and Settings\Ruben\java\lwjgl\native"

stupid mistake, but that means Netbeans doesn't check the input, and just ignores the part it doesn't understand :?

Fool Running

So did you get it working with that change? I couldn't tell if you are still having problems or not. :?
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

hedphuq