Я программирую программу отслеживания видео в обработке. Кто-нибудь знает, почему я получаю исключение null-указателя при увеличении int numberofblobs на 1. Начальное значение равно двум.Обработка - Исключение Null указателя при входе в цикл
Это весь код, чтобы обеспечить определенный контекст. Я очистил большинство неуместных вещей.
int numberofblobs;
void setup()
{
numberofblobs = 2;
// Size of applet
size(640, 480);
// BlobDetection
// img which will be sent to detection (a smaller copy of the cam frame);
img = new PImage(80, 60);
theBlobDetection = new BlobDetection(img.width, img.height);
theBlobDetection.setPosDiscrimination(false);
theBlobDetection.setThreshold(0.9f); // will detect bright areas whose luminosity > 0.2f;
}
// ==================================================
// captureEvent()
// ==================================================
void captureEvent(Capture cam)
{
cam.read();
newFrame = true;
}
// ==================================================
// draw()
// ==================================================
void draw()
{
if (!starttracking) {
for (int i = 0; i<10; i++) {
if (UNLOCKED[i])
starttracking = true;
}
}
if (newFrame)
{
newFrame=false;
image(cam, 0, 0, width, height);
img.copy(cam, 0, 0, cam.width, cam.height,
0, 0, img.width, img.height);
theBlobDetection.computeBlobs(img.pixels);
//if (starttracking) {
locateBlobs();
//}
}
println("numberofblobs:");
println(numberofblobs);
}
// ==================================================
// locateBlobs()
// ==================================================
void locateBlobs()
{
noFill();
Blob b;
for (int n=0; n<theBlobDetection.getBlobNb(); n++)
{
b=theBlobDetection.getBlob(n);
if (n<numberofblobs) {
println(n);
vectors[n].x = b.xMin*width;
vectors[n].y = b.yMin*height;
println(n);
// Blobs
strokeWeight(1);
stroke(255, 0, 0);
ellipse(
vectors[n].x, vectors[n].y, 80, 80 //b.xMin*width-(b.w*width),b.yMin*height-(b.h*height),30,30
);
}
}
}
void keyPressed() {
if (key =='1') {
if (!UNLOCKED[0]) {
numberofblobs++;
}
UNLOCKED[0]=true;
} else if (key =='2') {
if (!UNLOCKED[1]) {
numberofblobs++;
}
}
}
Эта линия подсвечивается обработки на метание ошибки (в LocateBlobs()
):
vectors[n].x = b.xMin*width;
На попытке сузить вещи вниз дальше, я сразу получаю исключения нулевого указателя, без увеличения numberofblobs int. Выделенная линия остается неизменной.
Это еще более упрощен код:
int numberofblobs;
PVector[] vectors = new PVector[10];
void setup()
{
numberofblobs = 2; // max 10
// Size of applet
size(640, 480);
}
void draw()
{
println("numberofblobs:"); println(numberofblobs);
locateBlobs();
}
void locateBlobs()
{
noFill();
for (int n=0; n<10; n++)
{
// b=theBlobDetection.getBlob(n);
int i = n;
if (n<numberofblobs) {
println(n);
vectors[n].x = 10;
vectors[n].y = 10;
strokeWeight(1);
stroke(255, 0, 0);
//ellipse(
//vectors[n].x, vectors[n].y, 80, 80);
}
}
}
void keyPressed() {
if (key =='1') {
numberofblobs++;
}
}
где вы определяете numberofblobs? – chris
Я определил его в настройке – user1803370
, не могли бы вы добавить, как вы его объявляете в каком-то контексте, это похоже на проблему с областью – chris