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 › NullPointerException
Page Index Toggle Pages: 1
NullPointerException (Read 1016 times)
NullPointerException
Oct 8th, 2006, 10:51pm
 
I'm getting a NullPointerException at "screen[i] = pixels[i];" and figure it has something to do with needing to identify screen[]/pixels[] but how?


int screen[]; // a working copy of pixels[]
// (remember that pixels[] is an array provided by
// p5 that allows access to the pixel buffer)

void setup()
{
 size(600, 300);
 background(#ffffff);
 screen = new int[width*height];

 // copy the empty white screen into our screen array
 preserveScreen();
}

void loop()
{
 // repaint what was previously on screen
 restoreScreen();
}

void mouseDragged() {
 // draw a transparent square
 alphaFrame(mouseX, mouseY);

}

void keyReleased() {
 // clear screen
 fill(#ffffff);
 noStroke();
 rect(0, 0, width, height);

 // copy cleared screen into working copy of screen pixels
 preserveScreen();
}

// this function copies the pixels onscreen into screen[]
void preserveScreen() {
 for (int i = 0; i < width*height; i++) {
   screen[i] = pixels[i];
 }
}

// this function copies the values in screen[] onto the screen
void restoreScreen() {
 for (int i = 0; i < width*height; i++) {
   pixels[i] = screen[i];
 }
}

// this function draws a transparent square on screen[], our working array.
// based on source code written by MESCOBOSA AND REAS, with improvements
// by FORKINSOCKET.
void alphaFrame(float xin, float yin)
{
 int x = int(xin);
 int y = int(yin);
 int s = 100; // size of side of square

 s/=2; // halved to center square around xin, yin

 // iterate through screen pixels
 for(int i=x-s; i<x+s; i++) {
   for(int j=y-s; j<y+s; j++) {

     // make sure we don't try to draw outside the bounds of the screen
     if(i < width && i >= 0 && j>=0 && j < height) {

       int rgb = screen[j*width + i]; // gets the color val of current pixel

       // split into red, green, blue
       int r = (rgb >> 16) & 0xff;
       int g = (rgb >> 8) & 0xff;
       int b = rgb & 0xff;

       // slightly darken the color values of the pixels where the square is to be drawn
       if (r > 1) r--;
       if (g > 1) g--;
       if (b > 1) b--;

       // now put the updated values back in the working copy of pixels array
       screen[j*width + i] = 0xff000000 | (r << 16) | (g << 8) | (b);

     }
   }
 }
}
Re: NullPointerException
Reply #1 - Oct 8th, 2006, 11:10pm
 
I think you're just missing loadPixels().

http://www.processing.org/reference/loadPixels_.html
Re: NullPointerException
Reply #2 - Oct 8th, 2006, 11:30pm
 
Do I have to use an image? If so, that's fine.

Where does this code belong? Before setup?
int halfImage = width*height/2;
PImage myImage = loadImage("topanga.jpg");
image(myImage, 0, 0);

And does this go in setup?
loadPixels();
for(int i=0; ihalfImage; i++) {
 pixels[i+halfImage] = pixels[i];
}
updatePixels();
Re: NullPointerException
Reply #3 - Oct 10th, 2006, 6:31am
 
Heeeeelp!
Re: NullPointerException
Reply #4 - Oct 10th, 2006, 2:00pm
 
In setup you have a command called size(). This defines the width and height of the applet. How is your program going to know the width and height of the applet if you haven't told it yet (And no, size() is the first thing called in setup, nowhere else).

Use loadPixels() and updatePixels() depending on whether you have to access pixels[] for the first time or tell your program that the pixels have changed.

loop() is a deprecated command. Use draw() instead.

Your program required 3 changes to operate. Listed below:
Code:

int screen[];
void setup()
{
size(600, 300);
background(#ffffff);
screen = new int[width*height];
preserveScreen();
}
void draw() // <-- HERE ME CHANGEY GOODNESS
{
restoreScreen();
}
void mouseDragged() {
alphaFrame(mouseX, mouseY);
}
void keyReleased() {
fill(#ffffff);
noStroke();
rect(0, 0, width, height);
preserveScreen();
}
void preserveScreen() {
loadPixels(); // <-- HERE ME CHANGEY GOODNESS
for (int i = 0; i < width*height; i++) {
screen[i] = pixels[i];
}
}
void restoreScreen() {
for (int i = 0; i < width*height; i++) {
pixels[i] = screen[i];
}
updatePixels(); // <-- HERE ME CHANGEY GOODNESS
}
void alphaFrame(float xin, float yin)
{
int x = int(xin);
int y = int(yin);
int s = 100;
s/=2;
for(int i=x-s; i<x+s; i++) {
for(int j=y-s; j<y+s; j++) {
if(i < width && i >= 0 && j>=0 && j < height) {
int rgb = screen[j*width + i];
int r = (rgb >> 16) & 0xff;
int g = (rgb >> 8) & 0xff;
int b = rgb & 0xff;
if (r > 1) r--;
if (g > 1) g--;
if (b > 1) b--;
screen[j*width + i] = 0xff000000 | (r << 16) | (g << 8) | (b);

}
}
}
}
Re: NullPointerException
Reply #5 - Oct 10th, 2006, 7:53pm
 
Okay I see now. So it's all embedded in the methods.

Thank yooouuu.
Page Index Toggle Pages: 1