Loading...
Logo
Processing Forum
Hi All,

How to create an ArrayList with float[] and PImage[] structures?

I want to associate each pointers from kinect Osceletor to your float position (x,y,z) and your image.

For example: headCoords has x, y and z position and an cerebral image associated.

My code return NullPointerException...

Lets go:

I have a class Skeleton:

Copy code
  1. class Skeleton {
  2.       float headCoords[] = new float[3]
  3.       float neckCoords[] = new float[3]
  4.       
  5.       PImage headImg[] = new PImage[1];
  6.       PImage neckImg[] = new PImage[1];
  7.       
  8.       float[] allCoords[] = {headCoords, neckCoords};
  9.       PImage[] orgaos[] = {headImg, neckImg}
  10.       
  11.       int id;
  12.       
  13.       Skeleton(int id) {
  14.             this.id = id;
  15.       }
  16. }

in main i have:

Copy code
  1. import processing.opengl.*;

  2. import oscP5.*;
  3. import netP5.*;

  4. OscP5 oscP5;

  5. Hashtable<Integer, Skeleton> skels = new Hashtable<Integer, Skeleton>();

  6. void setup() {
  7.     size(screen.height*4/3/2, screen.height/2, OPENGL); //Keep 4/3 aspect ratio, since it matches the kinect's.
  8.     oscP5 = new OscP5(this, "127.0.0.1", 7110);
  9.     hint(ENABLE_OPENGL_4X_SMOOTH);
  10.     noStroke();
  11. }

  12. void oscEvent(OscMessage msg) {
  13.   msg.print();
  14.   
  15.   if (msg.checkAddrPattern("/joint") && msg.checkTypetag("sifff")) {
  16.     // We have received joint coordinates, let's find out which skeleton/joint and save the values ;)
  17.     Integer id = msg.get(1).intValue();
  18.     Skeleton s = skels.get(id);
  19.     if (s == null) {
  20.       s = new Skeleton(id);
  21.       skels.put(id, s);      
  22.     }   
  23.        
  24.     if (msg.get(0).stringValue().equals("head")) {
  25.       s.headCoords[0] = msg.get(2).floatValue();
  26.       s.headCoords[1] = msg.get(3).floatValue();
  27.       s.headCoords[2] = msg.get(4).floatValue();
  28.       s.headImg[0] = loadImage("data/1.png");
  29.       
  30.     }
  31.     else if (msg.get(0).stringValue().equals("neck")) {
  32.       s.neckCoords[0] = msg.get(2).floatValue();
  33.       s.neckCoords[1] = msg.get(3).floatValue();
  34.       s.neckCoords[2] = msg.get(4).floatValue();
  35.       s.neckImg[0] = loadImage("data/2.png");
  36.     }     
  37.   }
  38.   else if (msg.checkAddrPattern("/new_user") && msg.checkTypetag("i")) {
  39.     // A new user is in front of the kinect... Tell him to do the calibration pose!
  40.     println("New user with ID = " + msg.get(0).intValue());
  41.   }
  42.   else if(msg.checkAddrPattern("/new_skel") && msg.checkTypetag("i")) {
  43.     //New skeleton calibrated! Lets create it!
  44.     Integer id = msg.get(0).intValue();
  45.     Skeleton s = new Skeleton(id);
  46.     skels.put(id, s);
  47.   }
  48.   else if(msg.checkAddrPattern("/lost_user") && msg.checkTypetag("i")) {
  49.     //Lost user/skeleton
  50.     Integer id = msg.get(0).intValue();
  51.     println("Lost user " + id);
  52.     skels.remove(id);
  53.   }
  54.   
  55. }


  56. void draw()
  57. {
  58.   background(0);  
  59.   for (Skeleton s: skels.values()) {
  60.     for (float[] j: s.allCoords) {
  61.       for (PImage[] i: s.orgaos) {        
  62.               pushMatrix();
  63.               translate(j[0]*width, j[1]*height, -j[2]*300);
  64.               image(i[0], j[1], j[2]);
  65.               popMatrix();
  66.             }
  67.          }
  68.        }
  69.    }
  70. }

    
Any suggestion?

best from brazil.

Replies(4)

See the Why do I get a NullPointerException? article.
When you have such error, it is useful to point to the line highlighted by Processing. We might be able to find it by spending time in analyzing your code, but it would be faster to give such information beforehand.
In most cases, we cannot even run your code to reproduce the issue (we don't have the libraries, even less the hardware!).
Thanks PhiLho!

My question is:

How to create an array with float and PImage?

and How to access this array and use it?

in MotionCapture3D example has:

Copy code
  1. void draw() {
  2. for (Skeleton s: skels.values()) {
  3.     fill(s.colors[0], s.colors[1], s.colors[2]);
  4.     for (float[] j: s.allCoords) {
  5.        pushMatrix();
  6.         translate(j[0]*width, j[1]*height, -j[2]*300);
  7.         sphere(2 * ballSize/j[2]);
  8.         popMatrix();
  9.        }
  10.     }
  11. }
I want to change sphere to image.

the Skeleton class has an array only with float coords:

Copy code
  1. class Skeleton {
  2.   float headCoords[] = new float[3];
  3.   float neckCoords[] = new float[3];
  4.   
  5.   float[] allCoords[] = {headCoords, neckCoords};
  6.   int id;
  7.   Skeleton(int id) {
  8.     this.id = id;
  9.    }
  10. }
I want to put associated image in each item.

The coordinators is generated by Osceleton and get in OscEvent function:

Copy code
  1. void oscEvent(OscMessage msg) {
  2.   msg.print();
  3.   
  4.   if (msg.checkAddrPattern("/joint") && msg.checkTypetag("sifff")) {
  5.     // We have received joint coordinates, let's find out which skeleton/joint and save the values ;)
  6.     Integer id = msg.get(1).intValue();
  7.     Skeleton s = skels.get(id);
  8.     if (s == null) {
  9.       s = new Skeleton(id);
  10.       skels.put(id, s);      
  11.     }    
  12.        
  13.     if (msg.get(0).stringValue().equals("head")) {
  14.       s.headCoords[0] = msg.get(2).floatValue();
  15.       s.headCoords[1] = msg.get(3).floatValue();
  16.       s.headCoords[2] = msg.get(4).floatValue();      
  17.     }
  18.     else if (msg.get(0).stringValue().equals("neck")) {
  19.       s.neckCoords[0] = msg.get(2).floatValue();
  20.       s.neckCoords[1] = msg.get(3).floatValue();
  21.       s.neckCoords[2] = msg.get(4).floatValue();
  22.     }
  23.   }
  24.   else if (msg.checkAddrPattern("/new_user") && msg.checkTypetag("i")) {
  25.     // A new user is in front of the kinect... Tell him to do the calibration pose!
  26.     println("New user with ID = " + msg.get(0).intValue());
  27.   }
  28.   else if(msg.checkAddrPattern("/new_skel") && msg.checkTypetag("i")) {
  29.     //New skeleton calibrated! Lets create it!
  30.     Integer id = msg.get(0).intValue();
  31.     Skeleton s = new Skeleton(id);
  32.     skels.put(id, s);
  33.   }
  34.   else if(msg.checkAddrPattern("/lost_user") && msg.checkTypetag("i")) {
  35.     //Lost user/skeleton
  36.     Integer id = msg.get(0).intValue();
  37.     println("Lost user " + id);
  38.     skels.remove(id);
  39.   }
  40.   
  41. }

I want to create an list in Skeleton class with float data and specific PImage to show in draw().

Thanks a lot.
You wouldn't put all of that data in one array, but rather split it up into two; or, even better, create a class for it.
See the OOP Tutorial...
As said, the best way is to make a simple wrapper class that holds both the float and the PImage and anything you want to put there.