Nullpointerexception 正在处理-在for循环中增加时出现空指针异常
我正在编写一个视频跟踪程序。 有人知道为什么我在将int numberofblobs增加1时收到空指针异常吗。初始值为2 这是提供一些上下文的完整代码。我清理了大部分不相关的东西Nullpointerexception 正在处理-在for循环中增加时出现空指针异常,nullpointerexception,int,processing,Nullpointerexception,Int,Processing,我正在编写一个视频跟踪程序。 有人知道为什么我在将int numberofblobs增加1时收到空指针异常吗。初始值为2 这是提供一些上下文的完整代码。我清理了大部分不相关的东西 int numberofblobs; void setup() { numberofblobs = 2; // Size of applet size(640, 480); // BlobDetection // img which will be sent to detection (a s
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++;
}
}
}
在尝试进一步缩小范围时,我立即得到null指针异常,而不增加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++;
}
}
int numberofblobs;
PVector[]向量=新PVector[10];
无效设置()
{
numberofblobs=2;//最大值为10
//小程序的大小
尺寸(640480);
}
作废提款()
{
println(“numberofblobs:”);println(numberofblobs);
locateBlobs();
}
void locateBlobs()
{
noFill();
对于(int n=0;n,在显示的代码中,您不创建任何PVector
。除非它们是在其他地方创建的,否则这可能就是您获得NPE的原因
PVector是一个对象,您需要在访问其字段之前创建它们
在:
您创建的PVector类型数组包含10个null
“对象”,但根本没有PVector
你也许应该这样做
for(int i = 0; i< vectors.length; i++){
vectors[i] = new PVector();
}
或者,您可以在locateBlobs func中创建它们,如:
if (n<numberofblobs) {
vectors[n] = new PVector(10,10);
// blah :)
}
if(n多亏了前面的答案,我找到了问题
在for循环中,创建Pvectors时,我使用numberofblobs作为约束。这样,如果numberofblobs增加,可用的向量就太少了
因此,最初:
for(int i = 0; i< numberofblobs; i++){
vectors[i] = new PVector();
}
for(int i=0;i
应该是
for(int i = 0; i< maxnumberofblobs; i++){
vectors[i] = new PVector();
}
for(int i=0;i
在哪里定义numberofblobs?我在设置中定义了numberofblobs。您可以添加如何为某些上下文声明它,这似乎是一个范围问题。我已经添加了整个程序的清理版本。我希望这有助于您为void locateBlobs()列出两个单独的代码块
哪些不相同,您使用的是哪一个?事实上,这一部分在那里,但我在尝试简化代码时意外删除了它。这就是为什么我在简化后立即出现错误的原因。然而,您的回答让我找到了创建向量的for循环。这帮助我解决了它,因此得到了答案欧内斯。
if (n<numberofblobs) {
vectors[n] = new PVector(10,10);
// blah :)
}
for(int i = 0; i< numberofblobs; i++){
vectors[i] = new PVector();
}
for(int i = 0; i< maxnumberofblobs; i++){
vectors[i] = new PVector();
}