We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Help with error "NullPointerException"
Page Index Toggle Pages: 1
Help with error "NullPointerException" (Read 4797 times)
Help with error "NullPointerException"
Aug 28th, 2009, 11:32am
 
Hi, I've got a new different doubt (sorry about asking so much xD but I'm quite a newbie...). I'm using blob detection and everything seems to work ok. I managed to draw the centroids of the detected blobs to check if everything was working OK and it did. Now I've got a new strange problem...

Using this code:


//Copy centroids into an array
for (int ii= 0; ii<myDetector.getBlobNb () ; ii++){
 Blob myBlob = myDetector.getBlob(ii);            // Check every detected blob
 if(((myBlob.w*width)*(myBlob.h*height)) > 1500){ // if the blob is bigger than 1500 pixels
   
 centroid[centroidN][0] = myBlob.x*width;         // Save it's centroid into an array
 centroid[centroidN][1] = myBlob.y*height;
 centroidN = centroidN+1;
 
 }
}

I get this error in the line painted red:

NullPointerException

Exception in thread "Animation Thread" java.lang.NullPointerException
     at sketch_aug28a.draw(sketch_aug28a.java:118)
     at processing.core.PApplet.handleDraw(PApplet.java:1406)
     at processing.core.PApplet.run(PApplet.java:1311)
     at java.lang.Thread.run(Unknown Source)


I can run the program without any problem or error, but when a blob enters the frame it blocks and I get that error.

Does someone know why I might be getting it? :S
Re: Help with error "NullPointerException"
Reply #1 - Aug 28th, 2009, 11:36am
 
sounds awfully similar to this:

http://processing.org/discourse/yabb2/num_1251480942.html

that help any
Re: Help with error "NullPointerException"
Reply #2 - Aug 28th, 2009, 12:01pm
 
Thanx BenHem!! it did help ^^ I got rid of my error, but now I'm getting:

ArrayIndexOutOfBoundsException : 2

Exception in thread "Animation Thread" java.lang.ArrayIndexOutOfBoundsException: 2
     at sketch_aug28a.draw(sketch_aug28a.java:118)
     at processing.core.PApplet.handleDraw(PApplet.java:1406)
     at processing.core.PApplet.run(PApplet.java:1311)
     at java.lang.Thread.run(Unknown Source)

Which PhiLho talks about in his reply to other post saying:

"Oh, and you want to put a limit on nodecount! Otherwise you will get another exception: ArrayOutOfBounds."

In my case I suppose that means I should limit "centroidN". But I think I already do :S. By limiting it, he means for example, setting it to 0 again some time right?

Before I check a new frame, my code ends with:

   centroidN = 0;
   updatePixels();

And I've made the program so it doesnt take into count every blob, just blobs bigger than 1500pixels... so it shouldnt exceed the limit I think, should it? :S

(Or maybe PhiLho means something else by putting a limit to that variable? )

Thanx again Smiley

PD: I've just checked something. The program works ok while there is only 1 blob, it only crashes when it detects 2 blobs or more.
Re: Help with error "NullPointerException"
Reply #3 - Aug 28th, 2009, 12:44pm
 
I guess the OutOfBound exception occurs because you've reached the limit of the centroid array when doing:

Code:
centroid[centroidN] = ... 



How big is your array?
Re: Help with error "NullPointerException"
Reply #4 - Aug 28th, 2009, 12:46pm
 
Louen wrote on Aug 28th, 2009, 12:01pm:
By limiting it, he means for example, setting it to 0 again some time right

In that case, it was to stop incrementing the counter... Smiley

Quote:
ArrayIndexOutOfBoundsException : 2
Here, 2 is the value of index causing the exception. That means you have an array of size 2, ie. accepting indexes of 0 and 1.
Re: Help with error "NullPointerException"
Reply #5 - Aug 28th, 2009, 12:57pm
 
LoL!! Im SO retarded xD thanks PhiLho... instead of creating:

centroid[999][2]

I had created

centroid[2][999]

10 points for Louen!!! xDD

Thanks again Smiley When I'm done I'll show you guys my project, let's see if you like it! You're being incredibly helpful. Thanks!!!!
Page Index Toggle Pages: 1