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.