I’m working on a custom particle system for a game I am beginning to work on. I have come upon a NullPointerException on my first test. I think I know why but I have no idea how I can fix it. Here is the related code:
ParticleSystem[] allParticles;
@Override
public boolean onTouchEvent(MotionEvent me){
Random rand = new Random();
allParticles[allParticles.length] = new ParticleSystem(
rand.nextInt(10)+30,randomColor(),(int)me.getX(),(int)me.getY(), //this is 137
rand.nextInt(10)+5,rand.nextInt(10)+5);
return true;
}
Error:
08-04 20:53:27.579: ERROR/AndroidRuntime(16805): java.lang.NullPointerException 08-04 20:53:27.579: ERROR/AndroidRuntime(16805): at com.laytproducts.pixelinvaders.GamePanel.onTouchEvent(GamePanel.java:37) 08-04 20:53:27.579: ERROR/AndroidRuntime(16805): at android.view.View.dispatchTouchEvent(View.java:3766) 08-04 20:53:27.579: ERROR/AndroidRuntime(16805): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863) 08-04 20:53:27.579: ERROR/AndroidRuntime(16805): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863) 08-04 20:53:27.579: ERROR/AndroidRuntime(16805): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863) 08-04 20:53:27.579: ERROR/AndroidRuntime(16805): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1746) 08-04 20:53:27.579: ERROR/AndroidRuntime(16805): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1117) 08-04 20:53:27.579: ERROR/AndroidRuntime(16805): at android.app.Activity.dispatchTouchEvent(Activity.java:2092) 08-04 20:53:27.579: ERROR/AndroidRuntime(16805): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1730) 08-04 20:53:27.579: ERROR/AndroidRuntime(16805): at android.view.ViewRoot.handleMessage(ViewRoot.java:1794) 08-04 20:53:27.579: ERROR/AndroidRuntime(16805): at android.os.Handler.dispatchMessage(Handler.java:99) 08-04 20:53:27.579: ERROR/AndroidRuntime(16805): at android.os.Looper.loop(Looper.java:143) 08-04 20:53:27.579: ERROR/AndroidRuntime(16805): at android.app.ActivityThread.main(ActivityThread.java:4701) 08-04 20:53:27.579: ERROR/AndroidRuntime(16805): at java.lang.reflect.Method.invokeNative(Native Method) 08-04 20:53:27.579: ERROR/AndroidRuntime(16805): at java.lang.reflect.Method.invoke(Method.java:521) 08-04 20:53:27.579: ERROR/AndroidRuntime(16805): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 08-04 20:53:27.579: ERROR/AndroidRuntime(16805): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 08-04 20:53:27.579: ERROR/AndroidRuntime(16805): at dalvik.system.NativeStart.main(Native Method)
That probably really isn’t very good coding, so that’s why I need some help. I think it has something to do with the fact that I am using allParticles.length as the position to add the system, is there a better way to do this? I already tried turning it into an ArrayList then doing .add; it also gave me a NullPointerException which also makes me think it has something to do with the other part of line 137.
SOLVED
Did this:
@Override
public boolean onTouchEvent(MotionEvent me){
Random rand = new Random();
for(ParticleSystem p : allParticles){
if(null == p){
p = new ParticleSystem(rand.nextInt(10)+30,randomColor(),
(int)me.getX(),(int)me.getY(),15,15);
Log.i("Pixel Invaders","ParticleSystem created: ");
break;
}
}
return true;
}
You have not initialized the
allParticlesarray. And even if you initialize it,then still you cannot access
allParticles[allParticles.length]element, since the array index is from 0 toallParticles.length-1.So your code has two problems.