Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Processing 数组中处理时出现空指针异常。什么都试过了_Processing - Fatal编程技术网

Processing 数组中处理时出现空指针异常。什么都试过了

Processing 数组中处理时出现空指针异常。什么都试过了,processing,Processing,我正在做一个2d侧滚游戏,我被卡住了。我正在为越来越多的对象编写类,玩家必须避免这些对象。但遗憾的是,我得到了一个Nullpointerexception,我不知道为什么。在我清理主代码并将其转换为类之前,整个过程都正常工作。我认为我正确初始化了数组,没有未定义的变量。在过去的几个月里,我一直在使用处理,所以我可能会监督一些事情 非常感谢你的帮助 public class Blockfield { private int Blockcount; private PImage B

我正在做一个2d侧滚游戏,我被卡住了。我正在为越来越多的对象编写类,玩家必须避免这些对象。但遗憾的是,我得到了一个Nullpointerexception,我不知道为什么。在我清理主代码并将其转换为类之前,整个过程都正常工作。我认为我正确初始化了数组,没有未定义的变量。在过去的几个月里,我一直在使用处理,所以我可能会监督一些事情

非常感谢你的帮助

 public class Blockfield {
    private int Blockcount;
    private PImage Blockpic;
    private Block block[];

  //Constructor
  public Blockfield (int Blockcount) {
    this.Blockcount = Blockcount; 
    //new array
    block = new Block [Blockcount];
    for ( int i=0; i < Blockcount; i++) {
      block[i] = new Block( width+Blockpic.width, random (height));
    }
  }


  //Draw method for this class
  public void draw () {
    for (int i =frameCount/100; i >0; i--) {
      image ( Blockpic, block[i].x, block[i].y);
      //moves blocks right to left
      block[i].x -=7 ;
      //spawns block when they leave the screen
      if (block[i].x < 0) {
        block[i] = new Block( width+Blockpic.width, random (height));
      }
    }
  }
}

class Block {
float x, y;

Block ( float x, float y) {
this.x= x;
this.y= y;
}
}

问题是,在分配
Blockpic
之前,我正在尝试访问
Blockpic.width
中的
Blockfield
构造函数。解决方案是在类的构造函数中加载映像

工作代码:

public class Blockfield {
    private int Blockcount;
    private PImage Blockpic;
    private Block block[];

  //Constructor
  public Blockfield (int Blockcount) {
    this.Blockcount = Blockcount; 
    Blockpic = loadImage("block2.png");
    //new array
    block = new Block [Blockcount];
    for ( int i=0; i < Blockcount; i++) {
      block[i] = new Block( width+Blockpic.width, random (height));
    }
  }


  //Draw method for this class
  public void draw () {
    for (int i =frameCount/100; i >0; i--) {
      image ( Blockpic, block[i].x, block[i].y);
      //moves blocks right to left
      block[i].x -=7 ;
      //spawns block when they leave the screen
      if (block[i].x < 0) {
        block[i] = new Block( width+Blockpic.width, random (height));
      }
    }
  }
}

class Block {
float x, y;

Block ( float x, float y) {
this.x= x;
this.y= y;
}
}
公共类区块字段{
私有int区块计数;
私人PImage Blockpic;
私人大厦【】;
//建造师
公共区块字段(int Blockcount){
this.Blockcount=Blockcount;
Blockpic=loadImage(“block2.png”);
//新阵列
块=新块[块计数];
for(int i=0;i0;i--){
图像(块pic,块[i].x,块[i].y);
//从右向左移动块
块[i].x-=7;
//当它们离开屏幕时生成块
if(块[i].x<0){
块[i]=新块(宽度+块图片宽度,随机(高度));
}
}
}
}
类块{
浮动x,y;
块(浮动x、浮动y){
这个.x=x;
这个。y=y;
}
}

谢谢大家的帮助

您使用的编程语言有名称吗?空指针发生在哪里?在分配
Blockpic
之前,您正在访问
Blockfield
构造函数中的
Blockpic.width
。您好。即时通讯使用处理2.2.1。空指针出现在块[i]=新块(宽度+块pic.width,random(高度));这是否意味着我必须在课堂上加载图像?我已经编写了类似的代码,足以在主循环中加载图像。我尝试将Blockpic设置为公共int,但仍然出现错误。
public class Blockfield {
    private int Blockcount;
    private PImage Blockpic;
    private Block block[];

  //Constructor
  public Blockfield (int Blockcount) {
    this.Blockcount = Blockcount; 
    Blockpic = loadImage("block2.png");
    //new array
    block = new Block [Blockcount];
    for ( int i=0; i < Blockcount; i++) {
      block[i] = new Block( width+Blockpic.width, random (height));
    }
  }


  //Draw method for this class
  public void draw () {
    for (int i =frameCount/100; i >0; i--) {
      image ( Blockpic, block[i].x, block[i].y);
      //moves blocks right to left
      block[i].x -=7 ;
      //spawns block when they leave the screen
      if (block[i].x < 0) {
        block[i] = new Block( width+Blockpic.width, random (height));
      }
    }
  }
}

class Block {
float x, y;

Block ( float x, float y) {
this.x= x;
this.y= y;
}
}