Why I receive a NullPointerException error? Line 52

edited January 2018 in Library Questions

The class should take the image and in the black pixels draw a rect of random color.

import processing.video.*;

Capture realTime;
ritrattoClass selfie;
PImage img;

void setup()
{
   size(640,480);
   background(0);
   textSize(40);
   text("Say Cheese and Press the Mouse", 5, width/3);
   realTime= new Capture(this,640,480); 
   realTime.start();
   loadPixels();
}

void captureEvent(Capture realTime)
{
  realTime.read();
}

void draw()
{
  //image(realTime,0,0);
  if (mousePressed == true) {
    realTime.stop();
    img.filter(THRESHOLD);
    image(img,0,0);
    save("selfie.jpg");
    selfie=new ritrattoClass(img);
    selfie.ritratto();
  }
}

void mousePressed() {
  img=realTime.copy(); //saveFrame("selfie.jpg");
}

-----------Class-------------

class ritrattoClass{

  PImage img;
  PImage foto;

  ritrattoClass(PImage foto){
    img=foto;} //nel programma foto sarà una variabile con l'immagine "selfie"


  void ritratto(){
      foto.loadPixels();
      for (int y=0; y<foto.height; y+=3)
      {
       for (int x=0; x<foto.width; x+=3)
       {
         int index = x+foto.width*y;
         color col= foto.pixels[index];
         if (col==0){
         fill(random(255),random(255),random(255));
         rect(x,y,3,3);
         }
       }
      }
    updatePixels();
  }
}

Answers

  • Edit post, highlight code, press ctrl-o to format.

    Which line shows the error?

  • Because foto on line 52 refers to foto on line 45 which you don't ever set.

    The foto on line 47 is completely different and doesn't exist outside the constructor.

    Delete line 45, you don't need it. Change foto in ritratto to img (which you DO set)

Sign In or Register to comment.