-2
我在處理它是一個球往上走,並灌進仿真製作的動畫,我有一個錯誤,說「空指針異常」它不斷出現,我會給出代碼,並顯示錯誤的地方:如何在處理中調試NullPointerException?
Obj[] objs = new Obj[100];
void setup(){
size(1000, 1000);
}
void draw(){
background(250);
for(int i = 0; i < objs.length; i++){
objs[i].render();
objs[i].up();
objs[i].run();
}
}
the error is at objs[i].render();
這裏是obj中的代碼:
class Obj{
float x;
float y;
float speed;
float pspeed;
float velocity;
void render(){
fill(0);
ellipse(x, y, 5, 5);
}
void up(){
x = random(-1000, 1000);
y = y-1010;
}
void run(){
speed = random(4, 6);
pspeed = speed;
velocity = 0.05;
while(y<random(700, 1100)){
y=y-speed;
speed=speed-velocity;
}
while(y>0){
y=y+pspeed;
pspeed = pspeed - velocity;
}
}
}
您沒有顯示要繪製的調用的上下文。我猜測NPE的原因是你沒有初始化objs的元素 –