java - NullPointerException 并且无法初始化处理中的草图错误

标签 java 2d simulation physics

我写了一个关于两个行星之间引力的物理模拟。它运行得非常好,所以我决定将它提升到一个新的水平,并使用数组和五个行星(圆圈)重写它。但是我的代码给出了奇怪的并且从来不一样的结果错误。初始化草图时出现 NullPointerException 错误或 VM 错误(没有描述,只是“Vm 错误无法初始化 skecth”和“查看帮助并排除故障”bullsh*t)该程序使用 txt 文件读取数据(仔细检查了一下,效果很好)。 我的数组名称和描述是

  • PVector - Pos 代表位置
  • PVector - Vel 代表速度
  • PVector - Acc 代表加速
  • PVector - Dist 代表距离
  • PVector - Dir 代表方向
  • float - Mass 代表 Mass

我的代码:

 public PVector[] Pos = new PVector[5];
public PVector[] Acc = new PVector[5];
public PVector[] Vel = new PVector[5];
public PVector[] Dist = new PVector[5];
public PVector[] Dir = new PVector[5];
public float[] Mass = new float[5];
void setup(){
 String Data[] = loadStrings("Data.txt");
 size(800,800);
 for(int g = 0;g < 5;g++){
   Pos[g] = new PVector(float(Data[g+1]),float(Data[g+6]));
   Vel[g] = new PVector(float(Data[g+12]),float(Data[g+17]));
   Mass[g] = float(Data[g+23]);
 }
}
void draw(){
 for (int i = 0;i < 5;i++){
   for (int f = 0;f < 5;f++){
     if(i !=f){
       if(Pos[i].x < Pos[f].x){Dir[f].x = 1;Dist[f].x = (Pos[f].x - Pos[i].x);}else{ // I get the error here
       if(Pos[i].x > Pos[f].x){Dir[f].x = -1;Dist[f].x = (Pos[i].x - Pos[f].x);}else{
       if(Pos[i].x == Pos[f].x){Dir[f].x = 0;Dist[f].x = 0;}}}
       if(Pos[i].y < Pos[f].y){Dir[f].y = 1;Dist[f].y = (Pos[f].y - Pos[i].y);}else{
       if(Pos[i].y > Pos[f].y){Dir[f].y = -1;Dist[f].y = (Pos[i].y - Pos[f].y);}else{
       if(Pos[i].y == Pos[f].y){Dir[f].y = 0;Dist[f].y = 0;}}}
       if ((Dist[f].x != 0)){
         Acc[i].x+=((6*((Mass[i]*Mass[f])/Dist[f].magSq())/10000000)/Mass[i])*Dir[f].x;// *6/1000000 is MY G constant
       }
       if ((Dist[f].y != 0)){
         Acc[i].y+=((6*((Mass[i]*Mass[f])/Dist[f].magSq())/10000000)/Mass[i])*Dir[f].y;
       }
     }
   }
   Vel[i].x = Vel[i].x + Acc[i].x;
   Vel[i].y = Vel[i].y + Acc[i].y;
   Pos[i].x = Pos[i].x + Vel[i].x;
   Pos[i].y = Pos[i].y + Vel[i].y;
   ellipse(Pos[i].x,Pos[i].y,10,10);
 }
}

最佳答案

您可以在此处创建大小为 5 的 PVector 数组:public PVector[] Dir = new PVector[5]; 。此时,索引 0-4 中有五次 null

因为您没有在此数组中创建新的 PVector,所以当您尝试访问此处 Dir[f].x 中的变量 x 时,您会收到错误,因为 Dir[f] 为 null,您无法访问 null 的变量 x -> NullPointerException。

在这一部分中,您将实例化一些数组

for(int g = 0;g < 5;g++){
   Pos[g] = new PVector(float(Data[g+1]),float(Data[g+6]));
   Vel[g] = new PVector(float(Data[g+12]),float(Data[g+17]));
   Mass[g] = float(Data[g+23]);
 }

您还应该为 DirAccDist 添加实例化

<小时/>

另请注意,您正在使用对象,而不是原始数据类型。 nullnew PVector(0,0)

不同 <小时/>

同样从“设计”的角度来看,以这种方式使用数组并不是一个好的方法。您应该创建自己的类 Planet,其中每个行星都保存有关其属性的信息,并在您的主类中处理它们之间的交互。

<小时/>

如何创建“空”或“零”变量而不是空值?只需创建它们即可:)

for(int g = 0;g < 5;g++){
   Pos[g] = new PVector(float(Data[g+1]),float(Data[g+6]));
   Vel[g] = new PVector(float(Data[g+12]),float(Data[g+17]));
   Mass[g] = float(Data[g+23]);
   Dir[g] = new PVector(0,0);
   Acc[g] = new PVector(0,0);
   Dist[g] = new PVector(0,0);
 }

PS:我不知道这个类到底是如何实现的,使用 new PVector()new PVector(0) 而不是 new PVector(0, 0) 也可能有效。

关于java - NullPointerException 并且无法初始化处理中的草图错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29217549/

相关文章:

c++ - 简单的二维碰撞检测c++

c - 数组会用在什么地方?

java - 在 Struts 2 中使用 OGNL 检查 "top"值的成员资格

java - 需要在java中将一个字符串分成两部分

java - 寻找继承解决方案

java - 如何用双缓冲在Java中显示Pacman的张嘴/闭嘴动画?

python - 不规则点到网格python

java - jdk7之前的Java实现的Iterator接口(interface)在哪里

python - Cocotb 在门级仿真中使用泛型/参数

c# - 用C#以一定概率触发事件