FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   ARGH? What IS this error?
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: ARGH? What IS this error?  (Read 322 times)
Jon Dean
Guest
Email
ARGH? What IS this error?
« on: Sep 3rd, 2003, 9:19pm »

Hi. I was creating a simple processing program to display some scanned artwork I've done (no big artistic code in this one). I can't get the program to run, and the error message that appears at the bottom of the screen ("NullPointerException") is not logged. The source code is nothing groundbreaking, so I'm not too concerned about someone stealing it =^.^=
Can anyone spot what I missed? Or tell me what the error means?
CODE FOLLOWS:
//Surreal Scans
/*This program was written for two reasons: one, I wanted to learn how to use images
in my programs, and two, I wanted to display what happens when objects with too many
dimensions are placed in a Canon scanner */
void setup()
{
  size(500, 500);
  background(255);
  noStroke();
  fill(255, 255, 255);
}
int m = 1;
BImage a = loadImage("heytony.JPG");
void loop()
{
  if (m == 1) {
    a = loadImage("heytony.JPG");
  } else if (m == 2) {
    a = loadImage("arminnatube.JPG");
  }
  image(a, 0, 0);
}
void mouseReleased()
{
  if (m > 11) {
    m = 1;
  }
  else {
    m = m +1;
  }
}
 
fry


WWW
Re: ARGH? What IS this error?
« Reply #1 on: Sep 3rd, 2003, 9:52pm »

loadImage should happen at the end of setup, it can't just hang out in-between things like that.  
 
this is the second time this one has come up in a week or two, so i think we may need to try and figure out a way to catch this to avoid the "argh"..
 
(there's also a possibility that this is fixed in 59, unless of course you're already using 59 )
« Last Edit: Sep 3rd, 2003, 9:52pm by fry »  
vent

WWW
Re: ARGH? What IS this error?
« Reply #2 on: Sep 3rd, 2003, 9:54pm »

I think the problem is that your calling the loadImage method outside of the setup or loop method.
 
Also, there is no need to repeatedly loadImage... you only need to do it once on setup. Give this a try:
 
Code:

 
int m = 2;
BImage a;
 
void setup()
{
  size(500, 500);
  background(255);
  noStroke();
  fill(255, 255, 255);
 
  if (m == 1) {
    a = loadImage("heytony.JPG");
  } else if (m == 2) {
    a = loadImage("arminnatube.JPG");
  }
  
}
 
void loop()
{
  image(a, 0, 0);
}
void mouseReleased()
{
  if (m > 11) {
    m = 1;
  }
  else {
    m = m +1;
  }
}

 
 
// guess I was a little to slow with my response
 
« Last Edit: Sep 3rd, 2003, 9:55pm by vent »  

http://www.shapevent.com/
Jon Dean
Guest
Email
Now I'm supposed to say hmmm... that's interesting
« Reply #3 on: Sep 4th, 2003, 12:09am »

I thought the setup set the introductory state for the program, and couldn't be changed once the program was running. I'm using 59, BTW. So, I guess I was wrong then? The point of the program is to allow the user to click on the picture and have it switch to the next one (eventually I'll advance to the concept of using things called "buttons" =^.^=). So, the mouseRelease() change proposed in zevan's response is capable of causing the desired effect through code in the setup()? Or did you just not realize that I was attempting to cut back on variable usage in my code and use a single variable to hold all pictures (this saves me several lines of code as well)? Then what does loop() do? Is it for output purposes only? This answered one question but gave me a ton more, as you can see. Also, I have a proposed feature change (it's not really a bug). In my program, the value of the image variable "a" was only set in the if-then statement immediately before it was called to be displayed. This coding idea stemmed from my belief that setup() was unchangable. The code, then, looked like this:
STARTCODE
if (m == 1) {
    a = loadImage("heytony.JPG");
  } else if (m == 2) {
    a = loadImage("arminnatube.JPG");
  }
ENDCODE
after which the picture was dsplayed. I had set "m" equal to one when the variable was declared, insuring that "a" would always have a value. The program wouldn't run, claiming that there was a chance "a" might not have a value. Could you possibly correct that sort of problem in the future? There's a workaround for my problem, but other coders might not be able to "correct" the "error" because of the nature of their program.
 
benelek

35160983516098 WWW Email
Re: ARGH? What IS this error?
« Reply #4 on: Sep 4th, 2003, 8:58am »

the problem you've encountered is that p5 looks at the fact that 'a' is only given a value if some condition occurs. it doesn't matter that the condition will occur. the easy way you can solve it, is by substituting...
 
Code:
if (m == 1) {  
    a = loadImage("heytony.JPG");  
  } else if (m == 2) {  
    a = loadImage("arminnatube.JPG");  
  }

 
...with...
 
Code:
a = loadImage("heytony.JPG");
if (m == 2) {  
    a = loadImage("arminnatube.JPG");  
  }

 
...that way, no matter what the outcome of the if() statement, 'a' will already have a value. or, if you don't want the possibility of loading an image twice, you could do the following:
 
Code:
String s = "heytony.JPG";
if (m == 2) {  
    s = "arminnatube.JPG";  
  }
a = loadImage(s);
 
Pages: 1 

« Previous topic | Next topic »