Pregunta

I tried hand_gesture_recognition of Blobscanner in Eclipse.
There's this line to get a red value of a pixel:

int  []  Background;  

void Set(PImage I){

    for(int i=0; i<I.width*I.height; i++){
        int iP=I.pixels[i];
        Background[i*3]=(int)red(iP);
    }

}

the last line being the reason for this error:

Exception in thread "Animation Thread" java.lang.NullPointerException  
    at processing.core.PApplet.red(Unknown Source)  
    at Test$PBGS.Set(Test.java:475)  
    at Test$PBGS.<init>(Test.java:466)  
    at Test.setup(Test.java:39)  
    at processing.core.PApplet.handleDraw(Unknown Source)  
    at processing.core.PApplet.run(Unknown Source)  
    at java.lang.Thread.run(Unknown Source)  

I'm guessing that this is related to the point where if you implement Processing in Eclipse, you've gotta change color type to int. So, is there any way to change/implement this red() method if you want to use Processing in Eclipse?

¿Fue útil?

Solución

NullPointerExceptions shouldn't be the result of a type conversion between color and int, and in any case red() returns a float, so your cast should work fine.

I'm assuming you initialized your array, because the stack trace seems to show that the error wasn't caused by that assignment specifically...

Looking at the Processing source for PApplet, I see this definition for red():

public final float red(int rgb) {
    return g.red(rgb);
}

where g is declared as

public PGraphics g;

Based on my quick scan through the source, it seems that g is only initialized in the init() method or in the public void size(final int w, final int h, String renderer, String path) method.

Looking at your stack trace, it appears that g has not been initialized yet. Did you call either init() or size() anywhere else in your code?

In PGraphics:

public final float red(int rgb) {
    float c = (rgb >> 16) & 0xff;
    if (colorModeDefault) return c;
    return (c / 255.0f) * colorModeX;
}

So nothing there could throw a NullPointerException, as it's all math. My first guess is that the PGraphics object that red() needs was never initialized.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top