Trouble with a null pointer exception or Target VM failed to initialize

Hi, I'm trying to write a simple program the counts the number of files in a folder, and displays them on screen. My code is below. I also have a question, if I turn this into a web app, are there security issues associated with accessing folders this way? Should I be attempting something like node.js instead?

PImage img;
PImage[] images;
int fileNum = 0;

void setup() {
  // set background
  size(800,800);
  // set Path
  String path = "Desktop/MarkAndrew/images/";
  //print number of files in images folder and the names of those files
  println("Listing all filenames in a directory: ");
  String[] filenames = listFileNames(path);
  fileNum = filenames.length;
  println(fileNum);
  println(filenames);
  // fill the images array with placeholders
  // assign an image from images to a placeholder in the array
  try {
  for ( int i = 0; i < images.length; i++ ){
    images[i] = loadImage(path + i + ".jpg" );   // make sure images "0.jpg" to "11.jpg" exist
    }
  } catch (Exception e){
    println(e);
  println("Hey, that’s not a valid index!");
}
}

//listfilenames function
String[] listFileNames(String dir) {
  File file = new File(dir);
  if (file.isDirectory()) {
    String names[] = file.list();
    return names;
  } else {
    // If it's not a directory
    return null;
  }
}

void draw() {
  // draw the images from the images folder on the screen
  for (int i= 0; i < images.length; i++) {
    img=images[i];
  // Displays the image at point (0, height/2) at half of its size
  image(img, i*110, 700, 100, 100);
  }
}

Answers

  • For starters, I don't think a JS allows loading and saving files from arbitrary paths! :-O
    It's pretty much confined to the "/data/" subfolder. Heck, even Android mode got problems w/ paths!

    Now about the NPE, you forgot to instantiate an array for variable images!
    So @ line #19, you try to access the field length, but it doesn't exist yet! (~~)

  • edited June 2014

    Should I be attempting something like Node.js instead?

    AFAIK, "Node.js" is a server, not some merely script which runs inside a browser!
    Rather, it generates a whole page w/ scripts which browsers can access & run!

    However, since it's a program which runs in a computer, it has access to any files that the host OS allows it!

  • ok, so here's the code, where I filled the images[] array with placeholders, so that I would get images.length. Are the values not retained globally? because I'm still getting a null pointer exception on the void draw() function.

    PImage img;
    PImage[] images;
    int fileNum = 0;
    
    void setup() {
      // set background
      size(800,800);
      // set Path
      String path = "Desktop/MarkAndrew/images/";
      //print number of files in images folder and the names of those files
      println("Listing all filenames in a directory: ");
      String[] filenames = listFileNames(path);
      fileNum = filenames.length;
      println(fileNum);
      println(filenames);
      // fill the images array with placeholders
      for (int i = 0; i < fileNum; i++){
      PImage images[] = new PImage[i];
      }
      // assign an image from images to a placeholder in the array
      try {
      for ( int i = 0; i < images.length; i++ ){
        images[i] = loadImage(path + i + ".jpg" );   // make sure images "0.jpg" to "11.jpg" exist
        }
      } catch (Exception e){
        println(e);
      println("Hey, that’s not a valid index!");
    }
    }
    
    //listfilenames function
    String[] listFileNames(String dir) {
      File file = new File(dir);
      if (file.isDirectory()) {
        String names[] = file.list();
        return names;
      } else {
        // If it's not a directory
        return null;
      }
    }
    
    void draw() {
      // draw the images from the images folder on the screen
      for (int i= 0; i < images.length; i++) {
        img=images[i];
      // Displays the image at point (0, height/2) at half of its size
      image(img, i*110, 700, 100, 100);
      }
    }
    
  • edited June 2014 Answer ✓

    Those variables, (img, images, fileNum), outside functions that you've placed at the top are called fields.
    They have a "global" scope throughout the sketch!

    On the other hand, variables declared inside functions or within {} curly brackets are called local.

    Now when a local variable is declared and it happens to have the same name as of a field,
    that field is temporarily overshadowed within the scoped lifespan of the local variable!

    That's exactly what happens @ line #18! You've ended up creating a local variable named images.
    Therefore, inside that for loop, it's the local variable being addressed rather than the field 1!

    By removing the type declaration part you avoid the field overshadowing
    and access the field images rather than some temporary local images!

    Read about how to instantiate an array at the link below:
    http://processing.org/reference/Array.html

Sign In or Register to comment.