繁体   English   中英

访问2D数组中的索引时返回空指针异常

[英]Null pointer exception returned when accessing index in 2D Array

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

例如,如果球在平台1-4上,它可以跳到平台5-8,反之亦然。 每个平台都是一个类,具有其x和y位置的属性(存储在PVector() )。 为了表示玩家在每个平台上所拥有的决策空间,我尝试制作一个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}};

我正在尝试使用2D数组模拟邻接列表,因为处理过程没有链接列表类。 当我接受用户的输入时,我会检查玩家是否在屏幕的一半上,然后我实际上是(尝试)使用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()函数调用中的decisionGraph[b.currentPlatform-1][4].pos.x

返回一个空指针异常。

是什么原因引起的? 如果有更好的方法来实现此功能,应该怎么做?

编辑:

触发初始化

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

触发类定义和平台创建

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,并在设置Trigger变量后在triggers()中构建decisionGraph,以便可以访问有效的非空对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM