# SpriteSheet Loader Nullpointer Why?

**URL:** <https://discourse.processing.org/t/spritesheet-loader-nullpointer-why/30334>\
**Category:** Coding Questions\
**Created:** [May 28, 2021, 10:41am UTC](https://discourse.processing.org/t/spritesheet-loader-nullpointer-why/30334 "2021-05-28T10:41:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tanghe](https://avatars.discourse-cdn.com/v4/letter/t/258eb7/32.png) [@Tanghe](https://discourse.processing.org/u/Tanghe)\
**Post date:** [May 28, 2021, 10:41am UTC](https://discourse.processing.org/t/spritesheet-loader-nullpointer-why/30334/1 "2021-05-28T10:41:37Z")

</div>

Hello so when i try using the code below i get a nullpointer on line 36 and i dont c why.  
Anny help is welcomed.

```auto
class Sprite
{
  int numRows, numCols;
  int posX, posY;
  int counter;
  PImage img;
  int partW, partH;
  int currWaitCtr, maxWaitCtr;
  PImage[][] states;
  
  Sprite(PImage img, int numRows, int numCols, int maxWaitCtr)
  {
    this(img, 0, 0, numRows, numCols, maxWaitCtr);
  }
  Sprite(PImage img, int posX, int posY, int numRows, int numCols, int maxWaitCtr)
  {
    this.img=img;
    this.numRows=numRows;
    this.numCols=numCols;
    this.posX=posX;
    this.posY=posY;
    this.partW=img.width/numCols;
    this.partH=img.height/numRows;
    this.counter=0;
    this.maxWaitCtr=maxWaitCtr;

    //state loader

    int w=this.img.width/this.numCols;
    int h=this.img.height/this.numRows;

    for (int r = 0; r < this.numRows; ++r) {
      for (int c = 0; c < this.numCols; ++c) {
        println("debug: " + "r =" +r+ " | c =" + c);
        this.states[r][c]=img.get(c*w, r*h, w, h);
      }
    }

  }

  void display()
  {
    int c = this.counter%this.numCols;
    int r = this.counter/this.numCols;
    int w=this.img.width/this.numCols;
    int h=this.img.height/this.numRows;

    PImage sprite = img.get(c*w, r*h, w, h);

    image(sprite, this.posX, this.posY);
  }

  void update()
  {
    this.currWaitCtr++;
    if(this.currWaitCtr>=this.maxWaitCtr)
    {
    this.counter++;
    this.counter%=this.numRows*this.numCols;
    this.currWaitCtr=0;
    }
  }
  void move(int deltaX, int deltaY, int rightBorder)
  {
   this.posX+=deltaX;
   this.posY+=deltaY;
   
   if(this.posX>rightBorder)
   {
    this.posX=-this.img.width/this.numCols; 
   }
  } 
}

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [May 28, 2021, 11:06am UTC](https://discourse.processing.org/t/spritesheet-loader-nullpointer-why/30334/2 "2021-05-28T11:06:58Z")

</div>

Need to declare the array like this (untested)

```auto
Sprite(PImage img, int posX, int posY, int numRows, int numCols, int maxWaitCtr)
  {
    this.img=img;
    this.numRows=numRows;
    this.numCols=numCols;
    this.posX=posX;
    this.posY=posY;
    this.partW=img.width/numCols;
    this.partH=img.height/numRows;
    this.counter=0;
    this.maxWaitCtr=maxWaitCtr;

   this.sprites = new PImage[this.nbrRows][this.nbrCols];
    //state loader

```
