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 › null if statement
Page Index Toggle Pages: 1
null if statement (Read 295 times)
null if statement
May 3rd, 2008, 2:29pm
 
hi i was wondering how you would go about checking if a value is null and if it is do a specific action.  I tried "if (x == null){"code"}" and it doesn't work and the is no sample codes that i have found using null so i am a bit confused.  

thanks

vince
Re: null if statement
Reply #1 - May 3rd, 2008, 3:28pm
 
You can only test if a variable is null if that variable is an Object type variable. You can't assign null values to primitive data types, so you can't test them either.

From your code, I don't know if 'x' is a primitive data type or not, but I assumed it was.

Null values are just a special value to indicate that a variable that normally would hold an object, doesn't hold anything.

Example:
Code:

PImage img;
void setup() {
img = null;
}

void draw() {
if (img != null) {
image(img, 0, 0);
}
}
void keyPressed() {
img = loadImage("image.jpg");
}

In the example, null is assigned to 'img' to indicate that it wasn't initialized yet (so it doesn't have a reference to a valid image for us to draw).
Page Index Toggle Pages: 1