PImage two-dimensional array problem reading null

I get null on line

 choseFrame();  
 PImage f = frame[selecMovie][selecImage];

Can someone help me find the problem? I don't know what it is the solution. Is the problem is how I load images or how I request to display the image. It looks like the image are loading.

java.io.File folder;

int totalPage=64; //number of page
float page=1;  
float pageNow; //the last limit
float pageAdd; // time gape in a page
float pageBefor; //the first limit

int numberM;
String [] MovieID; //name of the folder
int [] tFMovieID; //total image in folder
PImage[][] frame;
int numberFrame;
int numberMovie;
int[] x;
int[] y;

int selecImage;

void setup() {
  size(840, 640);
  smooth();
  frameRate(1);
  MovieID = new String[3];
  MovieID[0] = "tt0062622";// 2001  image 1.png....298.png
  MovieID[1] = "tt0074812"; // Logan's Run   image 1.png....238.png
  MovieID[2] = "tt0119177"; // Gattaca  image 1.png....213.png

  for (int i=0; i< MovieID.length; i++ ) {

    folder = new java.io.File(dataPath(MovieID[i]+"/"));
    String[] filenames = folder.list();
    numberFrame=filenames.length-1;

    frame=new PImage[MovieID.length][numberFrame];

    for ( int j = 1; j< numberFrame; j++ )
    {
      frame[i][j] = loadImage(dataPath(MovieID[i]+"/"+j+".png")); 
      println(frame[i][j]);
    }
  }

  x= new int[20];
  int moreX=0;
  for ( int i=0; i<x.length; i++ ) {
    x[i]=20+moreX;
    moreX=moreX+40;
  }
  y= new int[12];
  int moreY=0;
  for ( int i=0; i<y.length; i++ ) {
    y[i]=10+moreY;
    moreY=moreY+50;
  }
}

void draw() {
  background(255);
  int numXPos= int(random(1, 14));
  int numYPos= int(random(1, 9));
  int numXImg= int(random(6, 9));
  choseFrame(x[numXPos], y[numYPos], x[numXImg]);
}

void choseFrame(float imageX, float imageY, float imageSizeX) {

  pageNow= 100*page/totalPage; //the last limit
  pageAdd= 100*1/totalPage; // time for a page
  pageBefor= pageNow-pageAdd; //the first limit

  imageSizeX =imageSizeX-20;
  println("chose frame image size X", imageSizeX);

  selecImage = round ( random ( numberFrame*(pageBefor/100), numberFrame*(pageNow/100)));
  println("chose frame Selectimag ", selecImage);

  int selecMovie= int(random(0, MovieID.length));
  println("chose frame movie ", selecMovie);

  PImage f = frame[selecMovie][selecImage];
  println("f imgage ", frame[0][selecImage]);
  println("chose frame f ", f);

  float imageSizeY= (imageSizeX*f.height)/f.width;
  println("X1", imageX, "   add ", imageX+imageSizeX);

  if (imageX+imageSizeX> 780) {
    imageX= imageX- (  (imageX+imageSizeX)-780);
  }
  println("X2", imageX);
  if (imageY+imageSizeY> 560) {
    imageY= imageY- (  (imageY+imageSizeY)-560);
  }

  image(f, imageX, imageY, imageSizeX, imageSizeY);
}

Answers

  • It is probably caused when selecImage has the value 0 (zero) because in line 39 you don't load an image for the [0] element since the loop counter starts at 1

  • I realize I have basic mistakes. I have been suggested by a friend to only load images I need instead of loading all images and chose it randomly. I am looking into that option. Have a page[] that loads 1 to 3 image and from that page[] array the layout will be determined by the number of images inside. Instead of loading all images, choosing the layout and after that piking the image. i willget back at you to let you know how it whent. thanks

  • i stil try to know whats wrong i have remove more code to go near the problem. still have null pointer Exception image(frame[0][10], 0, 0);

    I see the image loading!

    }Screen Shot 2016-03-25 at 11.29.30

    java.io.File folder;
    
    String [] MovieID; //name of the folder
    int [] tFMovieID; //total image in folder
    PImage[][] frame;
    int numberFrame;
    int numberMovie;
    
    
    void setup() {
      size(840, 640);
      MovieID = new String[1];
      MovieID[0] = "tt0062622";// 2001  image 0.png....298.png
    
      numberMovie=MovieID.length;
    
      for (int i=0; i< numberMovie; i++ ) {
    
        folder = new java.io.File(dataPath(MovieID[i]+"/"));
        String[] filenames = folder.list();
        // if i dont do -2 it say that i have 300 elemets but only have 299 with the 0.png
        numberFrame=filenames.length-2;
    
        PImage[][] frame = new PImage[numberMovie][numberFrame];
    
        for ( int j = 0; j< numberFrame; j++ )
        {
          frame[i][j] = loadImage(dataPath(MovieID[i]+"/"+j+".png")); 
    
          println(i, " ", j, " ", frame[i][j]);
        }
        println(numberFrame);
      }
    }
    
    
    void draw() {
      background(255);
      image(frame[0][10], 0, 0);
    
  • Answer ✓

    You have 2 arrays called frame a global one at line 9 and another one local to the setup method in line 24. The local one stores the images but the global one is being used in the draw method.

    Changes line 24 to

    frame = new PImage[numberMovie][numberFrame];

    so that we have the global one only.

  • thanks quark

Sign In or Register to comment.