2014-09-26 92 views
0

一個NullPointerException被拋出這個代碼的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.yradius以前都在代碼中定義過,所以它們不爲空。我做了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) 
+2

也許中心爲空? – Blackbelt 2014-09-26 19:56:17

+1

請構建一個最小的測試用例。 – 2014-09-26 19:57:21

+1

你在哪裏登錄? - 在創建'float []'之前嘗試記錄。 – igr 2014-09-26 19:57:26

回答

0

看來centerradiusnull。你使用原始類型還是「大」類型?如果使用班級,自動​​簽名可能會給你NPE。

檢查這些變量中的任何一個被設置,也許它可以在日誌記錄之後,通過在同一時間發生的其他事件/線程來設置。嘗試同步這兩個操作:重置center/radius並製作數組。

+0

中心是一個對象,半徑是原始的float – vcapra1 2014-09-26 20:08:41

+0

你能解釋一下'center'可能會改變嗎? – igr 2014-09-26 20:09:25

+0

看來,如你所說,該中心正在變爲空。但是,center被定義爲類頂部的成員變量,並且可以在該函數內部進行訪問。如果我在函數的頂部重新定義它,它就會起作用。該變量從來沒有訪問,所以可以改變這個? – vcapra1 2014-09-26 20:18:26