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 t开发者_运维百科he 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 allParticles
array. And even if you initialize it,
allParticles=new ParticleSystem[10];
then still you cannot access allParticles[allParticles.length]
element, since the array index is from 0 to allParticles.length-1
.
So your code has two problems.
You are using allParticles.length
but you haven't initialised the array to anything. Your array currently points to null
.
I think what you are trying to achieve is to expand your array when you have something new to be inserted, but since Arrays need contiguous memory you cannot change its length at runtime. Try using ArrayList
instead. It works as an expandable array.
You are getting the exception from calling .length on allParticles. You have to initialize the array first. Even if that was working though, the call allParticles[allParticles.length] would be out of bounds because an array is indexed from 0 to length-1.
You can use a LinkedList because linkedlist is a dynamic data structure whereas arrays is static.
To still use arrays either you have to know beforehand how many elements you will enter into the array to initialize it or you will have to initialize it to a very large number. But this is a bad approach in software design.
精彩评论