Question

I have a ArrayList for dynamically creating enemies in a game. When populating the list intuitively, the game crashes suddenly, presumably with a null pointer exception.

The array is declared as such:

...
 ArrayList<EntityPlayer> EnemyList;

and populated with the enemy contents:

...
EnemyList.add(new EntityPlayer(mMVPMatrix, mActivityContext, R.drawable.enemy));

Suddenly, at this point, the program crashes with log:

03-30 01:03:02.066: W/dalvikvm(1348): threadid=11: thread exiting with uncaught exception (group=0xa6185908)
03-30 01:03:02.066: E/AndroidRuntime(1348): FATAL EXCEPTION: GLThread 102
03-30 01:03:02.066: E/AndroidRuntime(1348): java.lang.NullPointerException
03-30 01:03:02.066: E/AndroidRuntime(1348):     at com.example.rr.ApplicationClass.GLRenderClass.onDrawFrame(GLRenderClass.java:143)
03-30 01:03:02.066: E/AndroidRuntime(1348):     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1516)
03-30 01:03:02.066: E/AndroidRuntime(1348):     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)

The goal of this is to spawn a new enemy in an "if" statement, and use the methods stored in the element's instantiation of EntityPlayer. I can declare separate instances of EntityPlayer manually, but I have better control in this system. (Which is meant to mimic a vector class in C++)

Was it helpful?

Solution

Change ArrayList<EntityPlayer> EnemyList; to ArrayList<EntityPlayer> EnemyList = new ArrayList<EntityPlayer>();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top