How to fix loading texture error?

787 Views Asked by At

I am studying GL2, there is a function (from school) to load the texture:

bookTex = loadTexture(gl, "wattBook.jpg");

private Texture loadTexture(GL2 gl, String filename) {
    Texture tex = null;
    try {
      File f = new File(filename);
      BufferedImage img = ImageIO.read(f); // read file into BufferedImage
      ImageUtil.flipImageVertically(img);
      tex = AWTTextureIO.newTexture(GLProfile.getDefault(), img, false);
      // Different filter settings can be used to give different effects when the texture
      tex.setTexParameteri(gl, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);
      tex.setTexParameteri(gl, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_LINEAR);
    }
    catch(Exception e) {
      System.out.println("Error loading texture " + filename); 
    }
    return tex;
}

However, it always throws exceptions here: (it has too many rows of errors so I just copy the front lines)

*Error loading texture wattBook.jpg
Exception in thread "main-FPSAWTAnimator#00-Timer0" com.jogamp.opengl.util.AnimatorBase$UncaughtAnimatorException: java.lang.RuntimeException: com.jogamp.opengl.GLException: Caught NullPointerException: null on thread AWT-EventQueue-0
    at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWTAnimatorImpl.java:92)
    at com.jogamp.opengl.util.AnimatorBase.display(AnimatorBase.java:452)
    at com.jogamp.opengl.util.FPSAnimator$MainTask.run(FPSAnimator.java:178)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)
Caused by: java.lang.RuntimeException: com.jogamp.opengl.GLException: Caught NullPointerException: null on thread AWT-EventQueue-0
    at com.jogamp.common.util.awt.AWTEDTExecutor.invoke(AWTEDTExecutor.java:58)
    at jogamp.opengl.awt.AWTThreadingPlugin.invokeOnOpenGLThread(AWTThreadingPlugin.java:103)
    at jogamp.opengl.ThreadingImpl.invokeOnOpenGLThread(ThreadingImpl.java:201)
    at com.jogamp.opengl.Threading.invokeOnOpenGLThread(Threading.java:202)
    at com.jogamp.opengl.Threading.invoke(Threading.java:221)
    at com.jogamp.opengl.awt.GLCanvas.display(GLCanvas.java:505)
    at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWTAnimatorImpl.java:81)
    ... 4 more*

I am running this on Eclipse on mac, and the GL environment has been set. Can someone explain this error and how to fix it? Thanks!

0

There are 0 best solutions below