Getting a NullPointerException when trying to pass an ArrayList to another class
In my current project I have an ArrayList of PVectors that store xyz coordinates for 3d points. I'm passing the ArrayList to another class that manipulates it, however, I'm getting a NullPointerException when doing so. I'm assuming that one of the PVectors is null, but I checked for this by assigning any null objects to (0,0,0) and I'm still getting 开发者_如何学Pythonthe error. Also, is it more effecient to have an array of PVectors or an ArrayList of PVectors? Either way I'm still getting the error. Here is the line that produces it.
trip.pass(pointCoordinates);
And here is the main class
import org.openkinect.*;
import org.openkinect.processing.*;
Kinect kinect;
Trip trip;
boolean tripOn;
int w = 640;
int h = 480;
int distance = 5;
float[] depthLookUp = new float[2048];
ArrayList pointCoordinates = new ArrayList();
float factor = 400;
float a = 0;
float angle = 0;
float frequency = .05;
void setup() {
size(800,600,P3D);
kinect = new Kinect(this);
kinect.start();
kinect.enableDepth(true);
kinect.processDepthImage(false);
stroke(255);
for (int i = 0; i < depthLookUp.length; i++) {
depthLookUp[i] = rawDepthToMeters(i);
}
for(int i = 0; i < 31920; i++) {
pointCoordinates.add(new PVector(0, 0, 0));
}
}
void draw() {
background(0);
pushMatrix();
translate(width/2 + width/3,height/2, -200);
//add 1/3 width to account for rule of thirds
popMatrix();
int[] depth = kinect.getRawDepth();
calculate(depth);
if(!tripOn) {
for(int i = 0; i < pointCoordinates.size(); i++) {
PVector temp = (PVector) pointCoordinates.get(i);
point(temp.x, temp.y, temp.z);
}
}
if(frameCount % 10 == 0) {
if(tripOn) {
tripOn = false;
trip.clear();
}
else {
tripOn = true;
trip.pass(pointCoordinates);
}
}
if(tripOn) trip.run();
}
void stop() {
kinect.quit();
super.stop();
}
I can paste more classes if it helps to clarify the problem. Thanks!
You are not initializing your "trip" variable and therefore a call to trip.pass(..) would throw the NullPointerException.
You never seem to assign a value to the variable trip
. That would certainly cause a NullPointerException
at the line that you've shown.
From your code snippet its hard to get the source of your problem. But my first guess is a threading issue.
You are trying to use the trip.pass(pointCoordinates);
in a worker thread. Although it appears that ArrayList pointCoordinates = new ArrayList();
is not part of that thread.
A possible solution could be:
check whether the pointCoordinates
is initialized or not. If not then wait for it to get initialized.
Update
My bad. I have missed out the initialization of the trip object. :(
My +1 to Dan Breslau
and jerluc
精彩评论