# Null pointer exception

**URL:** https://discourse.processing.org/t/null-pointer-exception/4807
**Category:** Español
**Created:** [October 24, 2018, 11:40am UTC](https://discourse.processing.org/t/null-pointer-exception/4807 "2018-10-24T11:40:57Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![fonsinho](https://avatars.discourse-cdn.com/v4/letter/f/3be4f8/32.png) [@fonsinho](https://discourse.processing.org/u/fonsinho)
#### Post date: [October 24, 2018, 11:40am UTC](https://discourse.processing.org/t/null-pointer-exception/4807/1 "2018-10-24T11:40:57Z")

</div>

hola a todos, he pasado un pj5 a processing y me da una linea con null pointer exception…alguién puede ayudarme?

gracias

```auto
import processing.video.*;
Capture cam;
MyPoint[] pts;
PImage img;
int nbW = 40, nbH = 20;

 

void setup(){
    size(640,480, P2D);//OPENGL
  
 

   cam=new Capture(this,640,480);
   cam.start();
  
}

void initialize()
{
    pts = new MyPoint[nbW*nbH];
    for(int j=0; j<nbH; j++){
        for(int i=0; i<nbW; i++){
           pts[i + j*nbW] = new MyPoint(new PVector(i * width/nbW, j * height/nbH));
        }
    }
}

void draw(){
  
    pushMatrix();
    translate(width, 0);
    scale(-1,1);//mirror the video
   image(cam, 0, 0, width, height); //video is defined outside processing code
    popMatrix();
 
    img = get();
    img.resize(nbW, nbH);
    background(255);
    
    beginShape(TRIANGLES);
    //beginShape(QUAD);
    //texture(img);
    PVector a, b, c, d;
    float x1, x2, y1, y2;
    for(int j=0; j<nbH; j++){
        for(int i=0; i<nbW; i++){
            if(j<nbH-1 && i<nbW-1) 
            {           
            a = pts[i + j*nbW].pos;
            b = pts[i+1 + j*nbW].pos;
            c = pts[i+1 + (j+1)*nbW].pos;
            d = pts[i + (j+1)*nbW].pos;
              
            x1 = (a.x + b.x + d.x) / 3;
            x2 = (b.x + c.x + d.x) / 3;
            y1 = (a.y + b.y + d.y) / 3;
            y2 = (b.y + c.y + d.y) / 3;
            
            fill(img.get(int(x1/width*nbW), int(y1/height*nbH)));
            vertex(a.x, a.y);
            vertex(b.x, b.y);
            vertex(d.x, d.y);
            
            fill(img.get(int(x2/width*nbW), int(y2/height*nbH)));
            vertex(b.x, b.y);
            vertex(c.x, c.y);
            vertex(d.x, d.y);            
            /*
            vertex(a.x, a.y, int(a.x/width*nbW), int(a.y/height*nbH));
            vertex(b.x, b.y, int(b.x/width*nbW), int(b.y/height*nbH));
            vertex(c.x, c.y, int(c.x/width*nbW), int(c.y/height*nbH));
            vertex(d.x, d.y, int(d.x/width*nbW), int(d.y/height*nbH));
            */
            }
            pts[i + j*nbW].process();
        }
    }
    endShape();
}

class MyPoint
{
  final static float MAX_DIST_MOUSE = 80;//mouse influence zone
  final static float FRICTION_AIR = .92;//'air' FRICTION_AIR
  PVector target;//original position, MyPoint always tries to get back to it
  PVector f = new PVector(0, 0);//force applied to the point
  PVector pos, tmpv;
  int countExclude;

  MyPoint(PVector p_p)
  {
    pos = p_p;
    target = pos.get();
  }

  void process()
  {
    f.mult(FRICTION_AIR);   
    float d = dist(pos.x, pos.y, mouseX, mouseY);
    if (d < 6) d = 6;//prevent erratic behavior
    if (d < MAX_DIST_MOUSE)//mouse effect
    {
      tmpv = new PVector(mouseX, mouseY);
      tmpv.sub(pos);
      float a = 0.83 * cos(map(d, 0, MAX_DIST_MOUSE, 0, HALF_PI));
      tmpv.mult((mousePressed ? -1 : 1) * a / d);
      f.add(tmpv);
    }
    
    //attracted to its original position
    tmpv = target.get();
    tmpv.sub(pos);
    tmpv.mult(.03);
    f.add(tmpv);    
    pos.add(f);   
  }
}

```

---

<div class="post-metadata">

### Author: ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)
#### Post date: [October 24, 2018, 12:05pm UTC](https://discourse.processing.org/t/null-pointer-exception/4807/2 "2018-10-24T12:05:04Z")

</div>

Hola! Creo que olvidaste llamar

```
initialize();
```

---

<div class="post-metadata">

### Author: ![fonsinho](https://avatars.discourse-cdn.com/v4/letter/f/3be4f8/32.png) [@fonsinho](https://discourse.processing.org/u/fonsinho)
#### Post date: [October 24, 2018, 3:03pm UTC](https://discourse.processing.org/t/null-pointer-exception/4807/3 "2018-10-24T15:03:12Z")

</div>

pues si, ha funcionado…ahora tengo que conseguir que el grid se mueva con el mouse…gracias

---

<div class="post-metadata">

### Author: ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)
#### Post date: [October 24, 2018, 3:27pm UTC](https://discourse.processing.org/t/null-pointer-exception/4807/4 "2018-10-24T15:27:02Z")

</div>

Cómo encontré el fallo:

1. El Null Pointer Exception (NPE) lo daba en `a = pts[i + j*nbW].pos;` al intentar ejecutar el programa. NPE significa que se intentó acceder a una propiedad de un objeto nulo. En este caso significa que `pts[i + j*nbW]` es nulo, ya que no hay más objetos en esa linea.
2. Hay dos posibilidades. Una, que ese `pts` en concreto (el `i+j*nbW`) no estuviera inicializado. O dos, que ningún `pts` estuviera inicializado.
3. Me pregunté donde se inicializan los `pts`? encontré `initialize()` y la línea con `pts[...] = new MyPoint(...)`. Ahí deja de ser nulo `pts` y se le da un valor.
4. La pregunta entonces es quién llama `initialize()` y vi que nadie. Por lo tanto todos los `pts` son nulos y cualquier intento de acceder a una propiedad de algún `pts` fallaría con un NPE.

---

<div class="post-metadata">

### Author: ![fonsinho](https://avatars.discourse-cdn.com/v4/letter/f/3be4f8/32.png) [@fonsinho](https://discourse.processing.org/u/fonsinho)
#### Post date: [October 24, 2018, 3:29pm UTC](https://discourse.processing.org/t/null-pointer-exception/4807/5 "2018-10-24T15:29:13Z")

</div>

los no iniciados, siempre estamos con muchas lagunas
