GLu.gluUnProject Returning NullPointerException
I have gluUnProject set up to find world coordinates on touch like so:
public synchronized void randomMethod(MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_DOWN){
setx = event.getX();
sety = event.getY();
vector3(null);
}
}
public void vector3 (GL11 gl){
int[] viewport = new int[4];
float[] modelview = new float[16];
float[] projection = new float[16];
float winx, winy, winz;
float[] newcoords = new float[3];
gl.glGetIntegerv(GL11.GL_VIEWPORT, viewport, 0);
gl.glGetFloatv(GL11.GL_MODELVIEW_MATRIX, modelview, 0);
gl.glGetFloatv(GL11.GL_PROJECTION_MATRIX, projection, 0);
winx = (float)setx;
winy = (float)viewport[3] - sety;
winz = 0;
GLU.gluUnProject(winx, winy, winz, modelview, 0, projection, 0, viewport, 0, newcoords, 0);
posx = (int)newcoords[1];
posy = (int)newcoords[2];
posz = (int)newcoords[3];
}
newcoords is then used in the draw method to draw a square at touch point.
This code returns a NullPointerException however.
07-31 20:17:40.008: ERROR/AndroidRuntime(448): Uncaught handler: thread main exiting due to uncaught exception
07-31 20:17:40.098: ERROR/AndroidRuntime(448): java.lang.NullPointerException
07-31 20:17:40.098: ERROR/AndroidRuntime(448): at android.app.ui.GLSurfaceRenderer.vector3(GLSurfaceRenderer.java:65)
07-31 20:17:40.098: ERROR/AndroidRuntime(448): at android.app.ui.GLSurfaceRenderer.randomMethod(GLSurfaceRenderer.java:54)
07-31 20:17:40.098: ERROR/AndroidRuntime(448): at android.app.ui.Practice.onTouchEvent(Practice.java:37)
07-31 20:17:40.098: ERROR/AndroidRuntime(448): at android.app.Activity.dispatchTouchEvent(Activity.java:2064)
07-31 20:17:40.098: ERROR/AndroidRuntime(448): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
07-31 20:17:40.098: ERROR/AndroidRuntime(448): at android.view.ViewRoot.handleMessage(ViewRoot.java:1690)
07-31 20:17:40.098: ERROR/AndroidRuntime(448): at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 20:17:40.098: ERROR/AndroidRuntime(448): at android.os.Looper.loop(Looper.java:123)
07-31 20:17:40.098: ERROR/AndroidRuntime(448): at android.app.ActivityThread.main(ActivityThread.java:4310)
07-31 20:17:40.098: ERROR/AndroidRuntime(448): at java.lang.reflect.Method.invokeNative(Native Method)
07-31 20:17:40.098: ERROR/AndroidRuntime(448): at java.lang.reflect.Method.invoke(Method.java:521)
07-31 20:17:40.098: ERROR/AndroidRuntime(448): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-31 20:17:40.098: ERROR/AndroidRuntime(448): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-31 20:17:40.098: ERROR/AndroidRuntime(448): at dalvik.system.NativeStart.main(Native Method)
Line 54 is
vector3(nu开发者_C百科ll);
Line 65 is
gl.glGetIntegerv(GL11.GL_VIEWPORT, viewport, 0);
and line 37 from Practice class is
glSurfaceRenderer.randomMethod(event);
which is called inside the touchEvent that randomMethod is synchronised to.
Does anybody have any idea what is causing the null pointer exception
Thank You
GLU.gluUnProject
seems to be unrelated to the NullPointerException you get.
You are calling:
vector3(null);
so in function vector3(GL11 gl):
gl.glGetIntegerv(GL11.GL_VIEWPORT, viewport, 0);
throws the NPE because gl
is null.
You need to initialize the GL11 implementation, ie:
OpenGLContext glContext = new OpenGLContext(OpenGLContext.DEPTH_BUFFER);
GL11 gl = (GL11)glContext.getGL();
精彩评论