简体   繁体   中英

NullPointerException thrown for no reason

A NullPointerException is being thrown on line 33 of this code:

|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|  }

I know there are lots of reasons for NullPointerExceptions to be thrown, but I just don't see anything on that line causing an exception.

center.x , center.y , and radius are all defined previously in the code, so they are not null. I did a Log.v(TAG, "center.x: " + center.x + ", center.y: " + center.y + ", radius: " + radius) , just to check if they were defined, and this was the result: center.x: 0, center.y: 0, radius, 1

Here is the stack trace:

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)

It seems the center or radius is null . Do you use primitives or 'big' types? If using classes, autoboxing may give you NPE as well.

Check when any of those variables is set, maybe it can be set after the logging, by some other event/thread that happens in the same time. Try syncing these two actions: resetting the center / radius and making an array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM