Reputation: 413
I'm trying a very simple JOGL tutorial using NetBeans and Swing and it seems no matter how I arrange things I keep getting a null pointer exception. Here is the code I am using (adapted from a tutorial):
package testjogl;
import com.jogamp.opengl.*;
import com.jogamp.opengl.awt.GLCanvas;
import java.awt.Dimension;
public class OpenGLTest extends javax.swing.JFrame implements GLEventListener {
public OpenGLTest() {
initComponents();
}
public void doMain() {
GLCanvas canvas = new GLCanvas();
canvas.addGLEventListener(this);
canvas.setPreferredSize(new Dimension(640, 480));
this.getContentPane().add(canvas); // <--- This is where the exception happens
this.pack();
this.setVisible(true);
}
@Override
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
@Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
}
@Override
public void dispose(GLAutoDrawable drawable) { }
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new OpenGLTest().doMain();
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}
At the line where I add the canvas to my form, I get: Exception java.lang.NullPointerException(null) breakpoint hit in sun.awt.image.OffScreenImageSource at line 189 by thread AWT-EventQueue-0.
I've tried several different JOGL tutorials and all of them keep throwing this exception when I add the canvas to my form. I couldn't find the code to OffScreenImageSource, but when I step through, it dies at line 1119 in Container.java. The code is:
invalidateIfValid();
if (peer != null) {
comp.addNotify(); // <---- Dies right here
}
Does anyone have any ideas as to what might be causing this?
Upvotes: 1
Views: 320
Reputation: 6473
Your problem is you don't give enough time for Swing to initialize the component correctly with the OS GDI: As soon as you create the OpenGLTest() instance, you call doMain(). Give Swing some breath and it should work.
public static void main(String args[]) {
OpenGLTest test;
java.awt.EventQueue.invokeLater(() -> {
test = new OpenGLTest()
});
java.awt.EventQueue.invokeLater(() -> {
test.doMain();
});
}
FYI, I didn't validate the syntax.
Upvotes: 2