访问二维数组索引时返回空指针异常

Null pointer exception returned when accessing index in 2D Array

我目前正在处理 Processing 中的一款游戏,其中屏幕左右两侧有 3 个相等的 spaced 平台,屏幕底部有 2 个平台。游戏中的玩家是一个在其中一个平台上生成的球。当球在任意平台上时,玩家可以选择跳到屏幕另一半的4个平台中的任意一个。平台从屏幕左上角的平台开始到右上角的最后一个平台按1-8的顺序编号。

例如,如果球在平台 1-4,它可以跳到平台 5-8,反之亦然。每个平台都是一个 class,它具有 x 和 y 位置的属性(存储在 PVector() 中)。为了表示玩家在每个平台上的决定 space,我尝试制作了一个 8 x 8 矩阵,其中存储了玩家可以跳转到的 4 个触发器(平台)中的每一个(并且在它跳转到的位置上有空触发器)不能跳到)。这是矩阵:

Trigger[][]  decisionGraph = {{tNull,tNull,tNull,tNull,t5,t6,t7,t8},
                              {tNull,tNull,tNull,tNull,t5,t6,t7,t8},
                              {tNull,tNull,tNull,tNull,t5,t6,t7,t8},
                              {tNull,tNull,tNull,tNull,t5,t6,t7,t8},
                              {t1,t2,t3,t4,tNull,tNull,tNull,tNull},
                              {t1,t2,t3,t4,tNull,tNull,tNull,tNull},
                              {t1,t2,t3,t4,tNull,tNull,tNull,tNull},
                              {t1,t2,t3,t4,tNull,tNull,tNull,tNull}};

我试图使用二维数组模拟邻接列表,因为处理没有链表 class。当我接受用户的输入时,我会检查玩家在屏幕的哪一半,然后我基本上(试图)使用 map() 函数在球的当前位置和目标平台之间进行线性插值。这是一个案例:

 if (keyPressed) {
    if(b.currentPlatform < 5){
    if (b.grounded()) {
      if (key == 'q') {
        choice1 = true;
        
        triggerSpike = true;
        //interpolate ball from its current position to the position of the target platform
        b.pos.x = map(b.velo.x, 40,width-40 ,b.pos.x,decisionGraph[b.currentPlatform-1][4].pos.x);
        
        b.pos.y = map(b.velo.y,0,695,b.pos.y,decisionGraph[b.currentPlatform-1][4].pos.y);
        b.currentPlatform = 5;
      } 

出于某种原因,使用 decisionGraph[b.currentPlatform-1][4].pos.x

map() 函数调用中访问图形

returns 空指针异常。

可能导致问题的原因是什么?如果有更好的方法来实现这个功能,应该怎么做?

编辑:

触发初始化

Trigger t1;
Trigger t2;
Trigger t3;
Trigger t4;
Trigger t5;
Trigger t6;
Trigger t7;
Trigger t8;
Trigger t[];
//Null trigger
Trigger tNull;

触发器Class平台的定义和创建

class Trigger { //platforms to jump between
  PVector pos;
  PVector dim;
  Boolean isNull;

  Trigger(float x, float y, float w, float h) {
    pos = new PVector(x, y);
    dim = new PVector(w, h);
    isNull = false;
  }

  void draw() {
    pushMatrix();
    noStroke();
    fill(#00F9FF);
    rect(pos.x, pos.y, dim.x, dim.y);
    popMatrix();
  }
}

void triggers() {//hard coded platfomrs
  t1 = new Trigger(width/2 - 120, 695, 50, 10);
  t2 = new Trigger(width/2 + 120, 695, 50, 10);
  t3 = new Trigger(40, space*2.5 + 120, 10, 50);
  t4 = new Trigger(600, space*2.5 + 120, 10, 50);
  t5 = new Trigger(40, space*2.1, 10, 50);
  t6 = new Trigger(600, space*2.1, 10, 50);
  t7 = new Trigger(40, space, 10, 50);
  t8 = new Trigger(600, space, 10, 50);
  tNull = new Trigger(0,0,0,0);
  tNull.isNull = true;

单独添加下面这行代码导致异常

println("Decision Graph position: " + decisionGraph[b.currentPlatform-1][4].pos.x);

这个错误的原因是因为在decisionGraph初始化为数组的时候,你所有的Trigger变量都被初始化为null,所以它是一个空指针数组。这是因为您在其声明中为 decisionGraph 分配了一个值,该值发生在可以调用 triggers() 之前。

然后,您的部分代码会调用 triggers(),它会为触发器变量分配新值,但由于 decisionGraph 持有原始空指针的副本而不是对变量的引用,因此它不会更新.

要解决此问题,请在不使用初始值设定项的情况下声明 decisionGraph,并在设置触发器变量后在 triggers() 中构建 decisionGraph,以便它具有可供访问的有效非空对象。