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 & HelpSyntax Questions › nullpointerexception for background()
Page Index Toggle Pages: 1
nullpointerexception for background() (Read 864 times)
nullpointerexception for background()
Apr 15th, 2006, 3:42pm
 
I'm new to Processing and trying to load an image as a background.  The following code (which I copied from a reference page) generates a NullPointerException:

void setup() {
  size(1000,300);
  PImage b;
  b = loadImage("bubbles.jpg");
  background(b);
}

But the following code works --

void setup() {
  size(1000,300);
  PImage b;
  b = loadImage("bubbles.jpg");
  image(b,0,0);
}

Am I missing some subtlety of the background() method?  Thanks.
Re: nullpointerexception for background()
Reply #1 - Apr 15th, 2006, 4:41pm
 
hello adrianne,

as far as I know you cannot send an PImage as an argument into background function. it will only accept RGB or HSB mode. check the reference.

if you want to load an image to the  background you can still do it as your second example. background is merely a function that draws a color on the screen, you can simulate it by drawing a rect as well.

rect(0,0,width,height,1);
// draw a rect size of the width,height with
// coordinates(0,0) and transparency of 1
Re: nullpointerexception for background()
Reply #2 - Apr 15th, 2006, 4:46pm
 
Both the API and example section claim it can be done (http://processing.org/learning/examples/backgroundimage.html) as such (with a PImage) but perhaps that feature has not been implemented yet?
Re: nullpointerexception for background()
Reply #3 - Apr 15th, 2006, 5:57pm
 
wow I didn't know that. in that case it could be that since your image is a jpeg and it is compressed in its nature, the java interpreter don't get the same size as your stage and don't draw it on the screen.

my suggestion would be try to load an raw image and see what it does. or you can try to get the size of the jpeg and maybe make it equal to stage size some how.

sorry can't much of an help.
Re: nullpointerexception for background()
Reply #4 - Apr 15th, 2006, 8:05pm
 
Try it in P3D mode and it works. eg: size(1000, 300, P3D);

I don't know why, maybe it's a JAVA2D bug.

I did a speed test on background() vs image() and background is faster, so you should be using background() where possible.
Re: nullpointerexception for background()
Reply #5 - Apr 18th, 2006, 11:35pm
 
yup, that was a bug. now fixed.
Page Index Toggle Pages: 1