# 2d array expand()

**URL:** <https://discourse.processing.org/t/2d-array-expand/27732>\
**Category:** Beginners\
**Created:** [February 11, 2021, 10:49am UTC](https://discourse.processing.org/t/2d-array-expand/27732 "2021-02-11T10:49:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![HeyHo](https://avatars.discourse-cdn.com/v4/letter/h/a183cd/32.png) [@HeyHo](https://discourse.processing.org/u/HeyHo)\
**Post date:** [February 11, 2021, 10:49am UTC](https://discourse.processing.org/t/2d-array-expand/27732/1 "2021-02-11T10:49:26Z")

</div>

> Hey!  
> I just coded this little program:

```auto
int path [][] = new int[1][4];
//path = {{0,0,0,0}}
          
path [0][0] = -1;
//path = {{-1,0,0,0}}

path = (int[][])expand(path, path.length + 1);
//path = {{-1,0,0,0},  
// {0,0,0,0}}

println(path.length);
// prints 2

println(path[0].length);
// prints 4

path[1][0] = 1;
//should do:
//path = {{-1,0,0,0},  
// {1,0,0,0}}

```

> But when I try to run it I get an error at the last line(path[1][0] = 1;).  
> It says “NullPointerException”. 😟

> Can someone help me? 😀

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 11, 2021, 11:20am UTC](https://discourse.processing.org/t/2d-array-expand/27732/2 "2021-02-11T11:20:39Z")

</div>

I think, after expand, you need to init the new lines (tell them, they are arrays too)

**Sketch**

```auto
size(330, 330); 

int path [][] = new int[1][4];
//path = {{0,0,0,0}}

path [0][0] = -1;

path [0][2] = -1;
//path = {{-1,0,0,0}}

path = (int[][]) expand (path, path.length + 2);

// I think you need to init the new lines  
for (int i=0; i<path.length; i++)
  path[i] = new int[4];

//path = {{-1,0,0,0},  
// {0,0,0,0}}

println(path.length);
// prints 2

println(path[0].length);
// prints 4

path[1][0] = 1;
path[1][3] = 2;
path[2][0] = 3;

//should do:
//path = {{-1,0,0,0},  
// {1,0,0,0}}

int distFromBorder=40;

for (int i=0; i<path.length; i++) {
  for (int i2=0; i2<path[i].length; i2++) {
    text(path[i][i2], i*22+distFromBorder, i2*30+distFromBorder); 
    text("|", i*22+distFromBorder+10, i2*30+distFromBorder);
  }
}

```
