Exporting with pgraphics and nullpointerexception

Hello, so I’ve run into two problems. The first being that after a few hours (which varies depending on the machine that I’m running my code on), gives me a NullPointerException error, usually targeting filter shaders or pg.endDraw(). I’m pretty sure it has something to do with the array but can’t seem to figure out where the issue is. So instead of having the code run live, I’ve decided to saveFrame() and then convert into a movie, however, when doing this, it seems to miss some frames instead of the original frameRate. I tried using different frameRate but this didn’t seem to solve any issues.

import processing.video.*;

PGraphics pg;

PShader glitch;
//transition - fadein + fadeout
int time = 0;
int lastTime = 0; 
int fadeout = 0;
int fadein = 255; 
boolean trans = true;
boolean transition = true;
float transitionAlpha = 0.0;
int fade = 1;

PImage img;

int arr = 0;
float md = 0;
float mt = 0;
int idx = 0;

String[] movieType = {"v1_", "v2_", "v3_", "v4_", "v5_", "v6_",  "v7_",  "v8_",  "v9_",  "v10_", "v11_"};
int seq = 0;
int rand = 0;

//11 sequence
//16 videos per sequence
Movie[][] video = new Movie[11][15];

void setup() {
  size(1920, 1200, P2D);
  //frameRate(30);
  background(0);

  for (int i = 0; i < video.length; i++) {
    for (int j = 0; j < video[i].length; j++) {
      video[i][j] = new Movie(this, movieType[i]+j+".mp4");
    }
  }
  rand = int(random(video.length));
  video[seq][rand].play();
  //println(movieType[seq]+rand);

  //pgraphics 
  pg = createGraphics(width, height, P3D);
  noStroke();

  img = loadImage("texture.jpeg");

  //shaders
  glitch = loadShader("shrtoy.frag");
  glitch.set("u_resolution", float(width), float(height));
  glitch.set("u_channel1", video[seq][rand]);

  rectMode(CORNER);
}

void draw() {
  glitch.set("u_time", millis() / 1000.0);
  glitch.set("u_channel0", video[seq][rand]);

  background(0);


  if (video[seq][rand].time() > floor(video[seq][rand].duration())) {
    video[seq][rand].stop();
    seq++;
    rand = floor(random(video.length));
    if (seq >= movieType.length) { 
    seq = 0; 
  }
    video[seq][rand].play();
    //println(seq, rand);
  }

  //image(video[seq][rand], 0, 0, width, height);
  


  pg.beginDraw();
  Timer();
  tAlpha();

  //if (transition == true) {
  pg.tint(255, 255);
  pg.filter(glitch);
  //pg.image(video[seq][rand], 0, 0, width, height);
  //} else {
  pg.tint(255, transitionAlpha)  ;
  //pg.filter(glitch);
  pg.image(video[seq][rand], 0, 0, width, height);
  //}
println(transitionAlpha);
  pg.endDraw();
  image(pg, 0, 0);
  
  //PNG EXPORT
  saveFrame("final-1/capstonevid-#################.png");
  
}

void tAlpha() {
  if ( transitionAlpha < 0   ) { 
    fade =  1; 
    transitionAlpha = 0;   
    trans = false;
    if (transitionAlpha == 0){
      Timer();
    }
  }
  if ( transitionAlpha > 255 ) { 
    fade = -1; 
    transitionAlpha = 255; 
    trans = false;
  }
  if ( trans ) { 
    Timer();
    transitionAlpha += fade;
  }
}

void Timer() {
  int timeDelay = int(random(60000, 120000));
  if (millis() >= time + timeDelay) {
    time += time;
    trans = !trans;
  }
}

void movieEvent(Movie m) {
  m.read();
}
1 Like

using a

saveFrame()

inside draw() ,
does that mean you want make 60 pictures a second?

  • for how long?
  • hard disk space?
1 Like

You might to consider wrapping if(video[seq][rand].available()) around your entire draw after the video selection phase so that you are never trying to interact with your selected video if the frame isn’t yet available.

https://processing.org/reference/libraries/video/Movie_available_.html

You can also use try/catch to see if you can catch the NullPointerException and continue, advancing to the next frame (again, if it is a loading race condition or something like that). These approaches assume that your problem is an intermittent race condition.

Another possibility is that this line isn’t doing what you think it is doing

rand = floor(random(video.length));

and that somehow one of your video sequences cannot be accessed through play. Remove the random element and try looping through the videos, printing the name, then playing each one. If it fails on the same video every time you have found the problem.