【发布时间】:2014-09-26 19:54:40
【问题描述】:
在此代码的第 33 行抛出了一个 NullPointerException:
|31| @Override
|32| public float[] getVertices() {
|33| return new float[] {
|34| center.x - radius, center.y - radius, 0, 1,
|35| center.x + radius, center.y + radius, 1, 0,
|36| center.x - radius, center.y + radius, 0, 0,
|37| center.x - radius, center.y - radius, 0, 1,
|38| center.x + radius, center.y - radius, 1, 1,
|39| center.x + radius, center.y + radius, 1, 0
|40| };
|41| }
我知道NullPointerExceptions 被抛出的原因有很多,但我只是没有看到该行有任何导致异常的内容。
center.x、center.y 和 radius 都是之前在代码中定义的,因此它们不为空。我做了一个Log.v(TAG, "center.x: " + center.x + ", center.y: " + center.y + ", radius: " + radius),只是为了检查它们是否已定义,结果如下:center.x: 0, center.y: 0, radius, 1
这是堆栈跟踪:
java.lang.NullPointerException
at com.vcapra1.breakoutgame.objects.Ball.getVertices(Ball.java:33)
at com.vcapra1.breakoutgame.objects.TextureObject.<init>(TextureObject.java:17)
at com.vcapra1.breakoutgame.objects.Ball.<init>(Ball.java:16)
at com.vcapra1.breakoutgame.BreakoutRenderer.onSurfaceCreated(BreakoutRenderer.java:66)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1509)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1248)
【问题讨论】:
-
也许中心为空?
-
请构建一个最小的测试用例。
-
您在何处/何时登录? - 在创建
float[]之前尝试记录。 -
@игор - 就在第 33 行之前(当日志在那里时是第 34 行)
-
为什么这么多人认为记录是要走的路?记录是为了,呃,记录。调试器用于调试。使用调试器,您会在 10 秒内找到它。为什么不用它?
标签: java android nullpointerexception