NullPointer Exception Issue

I have a code that I need running for several hours. After several minutes it stops responding altogether (and I have to force quit the application) and gives me the error "NullPointer Exception" and highlights line 106. I have not been able to solve this, it may be because it runs out of pixels to grab? I am not sure. Another issue I am having is that it is using both my USB camera and my built-in mac camera. How do I get it to only use the USB camera? final int kTotalGlitches=10; int currGlitch=0; PImage img; ArrayList glitches;

final int kGlitchState=0;
final int kCamState=1;

int state=kCamState;


// ---------------------------------

final int kGlitchTimeLapse=7000;  // 2 secs
final int kCamTimeLapse=5000;    //8 secs
int triggerTime;

// ---------------------------------


import processing.video.*; 
Capture cam; 
Capture video;



void setup() { 
  size(1024, 768); 
  imageMode(CENTER);

  glitches = new ArrayList <GlitchClass>();
  for (int i=0; i<kTotalGlitches; i++)
    glitches.add(new GlitchClass());

  cam = new Capture(this);
  cam.start();
  triggerTime=millis()+kCamTimeLapse;
  img = cam;
  video = new Capture(this, width, height);
  video.start();
} 

void draw() { 

  if (millis()>=triggerTime) {

    if (state==kCamState) {
      state=kGlitchState;
      triggerTime+=kGlitchTimeLapse;
      currGlitch=int(random(kTotalGlitches));  //Select new glitch
    } else {
      state=kCamState;
      triggerTime+=kCamTimeLapse;
    }
  }

  if (state==kCamState)
    image(cam, width/2, height/2);
  else {
    background(0);
    glitches.get(currGlitch).display();
  }
} 


void captureEvent(Capture camera) {
  camera.read();

  img = createImage(width, height, RGB);
  video.loadPixels();
  arrayCopy(video.pixels, img.pixels);
}


//A glitch is just two ellipses overlapping on top of each other with different
//colors and one being rotated by 90 degrees.
class GlitchClass {

  void display() {

    loadPixels();

    int randPos = 0;
    if (frameCount  % 100 == 0)
      randPos = (int)random(0, video.height -4);

    int randPosY = 0;

    // if (frameCount  % 100 == 0)
    randPosY = (int)random(20, 50);

    // Begin a loop for displaying pixel rows of 4 pixels height
    for (int y = 0; y < video.height -57; y++) {
      if (img != null) {
        img.loadPixels();

        // Put 4 rows of pixels on the screen
        if (frameCount % 50 == 0)
          randPosY = (int)random(20, 50);
        for (int x = 0; x < video.width; x++) {
          if (frameCount % 100 == 0)
            randPosY = (int)random(20, 50);
          if (y < video.height -4) {
            pixels[x + (y + 0 + randPosY)* width] = img.pixels[  (y + 0 )* video.width + randPos + x];
            pixels[x + (y + 1 + randPosY) * width] = img.pixels[ (y + 1) * video.width + randPos + 1 + x];
            pixels[x + (y + 2 + randPosY) * width] = img.pixels[ (y + 2) * video.width + randPos + 2 + x];
            pixels[x + (y + 3 + randPosY) * width] = img.pixels[ (y + 3) * video.width + randPos + 3 + x];

            pixels[x + (y + 4 + randPosY)* width] = img.pixels[  (y + 4 )* video.width + randPos + 4 + x];
            pixels[x + (y + 5 + randPosY) * width] = img.pixels[ (y + 5) * video.width + randPos + 5 + x];
            pixels[x + (y + 6 + randPosY) * width] = img.pixels[ (y + 6) * video.width + randPos + 6 + x];
            pixels[x + (y + 7 + randPosY) * width] = img.pixels[ (y + 7) * video.width + randPos + 7 + x];
          }
        }
      } else {
        break;
      }
    }

    updatePixels();
  }
}

Thanks!

Answers

  • Ctrl-t in the editor will indent the code probably.

    It's also an idea to always use {}s even for single line blocks. Relying solely on indentation for blocks when you're not very good at it and the forum chomps stuff is asking for trouble.

  • highlights line 106.

    Try using println or the debugger to print the values of pixels, img, img.pixels

    (Careful not to print too much because that'll break processing. Although given it's throwing an npe immediately afterwards you should be OK)

  • Ctrl-T is not doing anything. I'm using safari on a Mac, does that matter?

  • In the editor, not the forum.

  • edited March 2017 Answer ✓

    MacOS uses diff. key shortcuts. Generally, when we see any key shortcut w/ CTRL in it, replace it w/ Command key ⌘ for Mac: L-)

    1. https://en.Wikipedia.org/wiki/Control_key#Similar_concepts
    2. https://en.Wikipedia.org/wiki/Command_key
  • edited March 2017

    I get this message on the console window:

    java.lang.NullPointerException
        at Glitch_4_3$GlitchClass.display(Glitch_4_3.java:124)
        at Glitch_4_3.draw(Glitch_4_3.java:81)
        at processing.core.PApplet.handleDraw(PApplet.java:2418)
        at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1540)
        at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)
    
  • Alright, I fixed the formatting, I hope. I tried to run the debugger and got the code I posted above. Do you know what does that mean?

  • Answer ✓

    @AndresAlejandro

    I am not sure how your code runs. I am getting an error in my IDE. Please review your code again. Specifically, in the first few lines, you have this line: ArrayList glitches however something is missing. Check: https://processing.org/reference/ArrayList.html

    Please do one more thing. You formatted your code by few lines were missed at the beginning of your post. Please edit your post, select those lines and hit ctrl+o and ensure there is an empty line above.

    Also managing two cameras, your captureEvent should be:

    Please implement the following change as well in your movieEvent:

    void captureEvent(Capture camera) {
      camera.read();
    
        if(camera==video)
        img = video.get();
    
        //img = createImage(width, height, RGB);
        //video.loadPixels();
        //arrayCopy(video.pixels, img.pixels);
      }
    

    And remove line 33: img=cam

    This won't solve all the problems. Can you comment what you are doing in display?

    Kf

  • Did you try adding the println statements?

  • (You can't always trust the line numbers because they relate to the pre-processed Java code, not the pde file. Which is why it's dating line 124 when you don't have a line 124)

Sign In or Register to comment.