0

I'm a beginner and I'm writing this code for an assignment.

I know this looks like a duplicate, but know what a NullPointerException error means and I really don't understand how to fix this one, but I think it has something to do with the libraries that I imported; I'm really not sure. I would sincerely appreciate any advice at all.

This first part is my code and below it are my NullPointerException errors from the stack trace.

Please let me know if I need to specify anything else.

Code:

import processing.video.*;
String PATH = "/Project_2_Scetch/data/video_library/img4.mp4";
Movie mov;`enter code here`

PFont f;

Capture cam;
float xin, yin;
int maxImages = 8;
int imageIndex = 0;
int value = 0;
PImage img;
PImage[] images = new PImage[maxImages]; 
float positionX = xin;
float positionY = yin;

void setup() {
  size(1000, 650);
  smooth();
  noStroke();
  img = loadImage("img0.jpg");
  rectMode(CENTER);

f = createFont("Source Sans Pro", 20, true);

if (key == 's'){
  String[] cameras = Capture.list();

  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } else {
    println("Available cameras:");
    for (int i = 0; i < cameras.length; i++) {
      println(cameras[i]);
    }

    // The camera can be initialized directly using an 
    // element from the array returned by list():
    cam = new Capture(this, cameras[0]);
    cam.start();     
  }      

  for (int i = 0; i < images.length; i ++ ) {
    images[i] = loadImage( "img" + i + ".jpg" );}}

}
void movieEvent(Movie m) {
  m.read();
}
void draw() {
  background(50, 20, 120);
  {
  textSize(20);
  text("In this game, there is no such thing as sleep. You will be going about my", 150, 70);
  text("endless day either procrastinating, or catching up on homework", 200, 110 );
  fill(255);
  text("To procrastinate for endless fun use Free Mode", 288, 150);
  fill(255);
  text("To catch up on my homework and experience existential terror, use Adventure Mode", 95, 190);
  fill(255);

  text("Free Mode (Press J)", 175, 338);
  fill(255);

  text("Adventure Mode (Press K)", 579, 340);
  fill(255);}




  if (key == 'j') {
    background(50, 20, 120);
    text("To take intentionally terrible selfies, press the (A) key.", 150, 300);
    fill(255);
    text("To rescue pets, press the (B) key.", 150, 350);
    fill(255);}

  if (key == 'a') {
    img = loadImage("img1.jpg");
    image(img, width/4, height/6);
    background(50, 20, 120);
    text("If you would like to continue taking terrible selfies, press the (G) key.", 125, 320);
    text("If you would like to go back and do something else, press the (J) key.", 125, 350);
  }
  if (key == 'b'){
    img = loadImage("img6.jpg");
    background(50, 20, 120);
    text("If you would like to see more animals, press the (C) key.", 175, 320);
    text("If you would like to go back and do something else, press the (J) key.", 175, 370);
  }

  if (key == 'c'){
 image(mov, 0, 0);
  }

  if (key == 'e'){
   // img = loadImage("img5.jpg");
    background(50, 20, 120);  
    text("You seem like an animal lover. If you would like to watch Animal Planet, press the (D) key.", 50, 320);
    text("If you are not an animal lover, how dare you.", 50, 360);
    text("Press the spacebar to start over and rethink your life's choices.", 50, 390);
  }
 if (key == 'g'){
   img = loadImage("img7.jpg");
   background(50, 20, 120);  
   text("You seem to like taking selfies. If you would like to take some more funky fresh selfies,", 53, 300);
   text("then please hit the (H) key.", 53, 350);
  }
 if (key == 'h'){
   background(50, 20, 120); 
   img = loadImage("img4.jpg");
   text("Are you satisfied with this selfie? Would you like to take more selfies? If so, press the (M) key.", 40, 300);
   text("If not, then that's okay too. If you'd rather work on some homework, press the (K) key instead.", 40, 335);
  }

  if (key == 'm'){
   text("Hey. You like selfies. No judgement here. Press the (S) key and take your own selfies and keep them close.", 40, 300);
   text("If not, that's alright too. If you'd rather, press (K) to start on some homework :)", 40, 330);
 }

 if (key == 'd'){
    background(50, 20, 120);
    fill(50, 60, 170);
    text("Go on, click on the box, you animal!", 250, 160);
    fill(255);
    line(470, 0, 470, 530);
    line(0, 295, 355, 295);

  }
  //this is a hot mess too, but move onto the next thing.j
  if (key == 's'){
  if(cam.available() == true) {
    cam.read();
  }
     image(cam, 0, 0);
  // The following does the same, and is faster when just drawing the image
  // without any additional resizing, transformations, or tint.
  //set(0, 0, cam);
  }
 }


void mouseClicked() {
  if (mouseX < 470 && mouseY < 295){
    fill(255);
    rect(470, 295, 500, 340);
    link("http://www.animalplanet.com/tv-shows/"); 
 } 
}

NullPointerException error #1:

This shows up when I do the if(key == 'c'); function where I'm trying to play a video. I imported the Video library for this function.

Exception in thread "Animation Thread" java.lang.NullPointerException
    at processing.core.PGraphics.image(PGraphics.java:3765)
    at processing.core.PApplet.image(PApplet.java:12109)
    at Project_2_Scetch.draw(Project_2_Scetch.java:113)
    at processing.core.PApplet.handleDraw(PApplet.java:2399)
    at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1527)
    at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)

NullPointerException error #2: This shows up when I do the if(key == 's'); function, and here I'm trying to get the user's webcam to pull up. I imported the IPCapture library for this function.

Exception in thread "Animation Thread" java.lang.NullPointerException
    at Project_2_Scetch.draw(Project_2_Scetch.java:152)
    at processing.core.PApplet.handleDraw(PApplet.java:2399)
    at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1527)
    at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • You need to initialize the `mov` variable. The one you pass to `image()` is `null` – dovetalk Mar 19 '16 at 15:51
  • for the second, I assume the `Capture.list()` call returns `null`, and not an empty list, so your first test after the call should be `if (cameras==null || cameras.length == 0)` – dovetalk Mar 19 '16 at 15:53
  • I suspect you need to do something where it says \`enter code here\` – Peter Lawrey Mar 19 '16 at 15:56

1 Answers1

1

You have two variables you aren't initializing: mov and cam.

The initialization of the mov variable looks like something you're supposed to complete for the assignment. You have to finish that part before you can use that variable.

You have an initialization of the cam variable, but it's inside an if statement. But notice that the if statement will never be entered, since user input isn't detected until after the setup() function finishes.

Also note that you shouldn't be calling loadImage() from the draw() function. That's going to load an image 60 times per second, which will slow your sketch down. Instead, load all of your images are the beginning of the sketch, in the setup() function. Store each image in a variable, and then just use those variables when you need them later.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107