首页 > 解决方案 > 处理 - ArrayList.add nullpointerexception

问题描述

这是整个处理文件 -

float camX = 0;
float camY = 0;
float camZ = 0;
float camS = 1;
float camA = 0;

float dist(float x,float y){
  return sqrt(pow(x,2)+pow(y,2));
}
float angle(float x,float y){
  if(x<0){
        return (3*PI/2)-atan(y/x);
    } else {
        return (PI/2)-atan(y/x);
    }
}



class Vert{
  float x;
  float z;
  Vert(float x, float z){
    this.x=x;
    this.z=z;
  }
  float getX(){
    return x;
  }
  float getZ(){
    return z;
  }
}
class Shape{
  float x;
  float y;
  float z;
  ArrayList<Vert> verts;
  float h;
 Shape(float x,float y,float z,ArrayList<Vert> verts,float h){
   this.x=x;
   this.y=y;
   this.z=z;
   this.verts = verts;
   this.h = h;
 }
 float getX(){
   return x;
 }
 float getY(){
   return y;
 }
  float getZ(){
   return z;
 }
 ArrayList<Vert> getVerts(){
   return verts; 
 }
 float getHeight(){
 return h;
}
ArrayList<Vert> getSlice(float sy){
   if(sy>=y&&sy<=y+height){
      return verts;
    } else {
     return null; 
    }
}

}


ArrayList<Shape> shapes;


Vert transVert(float vx,float vy,float vz,float cx,float cy,float cz,float cs,float ca){
  float a = angle(cx-vx,cz-vz);
  float d = dist(cx-vx,cz-vz);
  println(a+" , "+d);
  return new Vert((width/2)+(cx+sin((a+ca))*d*cs),(height/2)-(cz+cy+vy+cos((a+ca))*d*cs));
}


void drawSlice(float sy,float cx,float cy,float cz,float cs,float ca){
  for(int si = 0;si < shapes.size();si ++){
    Shape shape = shapes.get(si);
    ArrayList<Vert> slice = shape.getSlice(sy);
    beginShape();
    for(int i = 0;i < slice.size();i ++){
      Vert vi = transVert(slice.get(i).getX()+shape.getX(),sy+shape.getY(),slice.get(i).getZ()+shape.getZ(),cx,cy,cz,cs,ca);
      vertex(vi.getX(),vi.getZ());
    }
    endShape(CLOSE);
  }
}

void drawSpace(float y1,float y2,float res,float cx,float cy,float cz,float cs,float ca){
   for(float sy = y1;sy < y2;sy += res){
     drawSlice(sy,cx,cy,cz,cs,ca);
   }
}

void setup(){
  size(400,400);
  ArrayList<Vert> vs = new ArrayList<Vert>();
  vs.add(new Vert(-50,0));
  vs.add(new Vert(0,-50));
  vs.add(new Vert(50,0));
  vs.add(new Vert(0,50));
  shapes.add(new Shape(0,0,0,vs,20));
  shapes.add(new Shape(100,0,50,vs,20));
}
void draw(){
  background(255);
  drawSpace(-10,30,5,camX,camY,camZ,camS,camA);
}

我在shapes.add 行中遇到空点异常,但在vs.add 行中没有?spaes 数组是否超出范围?我需要它是一个公共数组列表

我在使用数组列表时遇到了很多以前从未遇到过的问题,因此为什么我要使用这种混乱的方法来添加单个元素而不是在一行中进行操作-所以我想任何建议都会有用-谢谢

标签: javaarraylistprocessingadd

解决方案


推荐阅读