LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: hedphuq on March 03, 2006, 10:06:12

Title: nullpointerexception lwjgl.opengl.GL11.glClear(G11.java:576)
Post by: hedphuq on March 03, 2006, 10:06:12
I've just started out with lwjgl using this tutorial (http://www.javalobby.org/members-only/game-dev/JavaGameProgramming-part1.pdf) 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();
       }
   }
}
Title: nullpointerexception lwjgl.opengl.GL11.glClear(G11.java:576)
Post by: hedphuq on March 03, 2006, 11:00:44
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
Title: nullpointerexception lwjgl.opengl.GL11.glClear(G11.java:576)
Post by: Odeamus on March 03, 2006, 11:38:41
I don't see a call to Display.create() anywhere. You need to call it before doing any opengl calls.

O.
Title: nullpointerexception lwjgl.opengl.GL11.glClear(G11.java:576)
Post by: hedphuq on March 04, 2006, 11:08:44
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
Title: nullpointerexception lwjgl.opengl.GL11.glClear(G11.java:576)
Post by: Odeamus on March 04, 2006, 21:21:59
All I can say is that you pasted the wrong code?

O.
Title: nullpointerexception lwjgl.opengl.GL11.glClear(G11.java:576)
Post by: hedphuq on March 05, 2006, 12:02:05
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
Title: nullpointerexception lwjgl.opengl.GL11.glClear(G11.java:576)
Post by: Odeamus on March 06, 2006, 08:41:01
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.
Title: nullpointerexception lwjgl.opengl.GL11.glClear(G11.java:576)
Post by: hedphuq on March 07, 2006, 18:44:47
did you compile it with netbeans IDE (5.0)?
Anyway, could you show me the VM options you used to run it?
Title: nullpointerexception lwjgl.opengl.GL11.glClear(G11.java:576)
Post by: Odeamus on March 08, 2006, 07:47:40
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.
Title: hmmmmmmmmm...
Post by: Fool Running on March 08, 2006, 14:43:17
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.
Title: nullpointerexception lwjgl.opengl.GL11.glClear(G11.java:576)
Post by: hedphuq on March 08, 2006, 18:32:02
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 :?
Title: hmmmmmm...
Post by: Fool Running on March 08, 2006, 19:21:50
So did you get it working with that change? I couldn't tell if you are still having problems or not. :?
Title: nullpointerexception lwjgl.opengl.GL11.glClear(G11.java:576)
Post by: hedphuq on March 09, 2006, 18:34:38
it's fixed now, thanks for your help!