Null type with variables

Hello there,

I want to do this :

I have a variable :

 color background_color;

and at the beginning, I don’t want to assign it because it’s an object property. Then if I call a fonction setWindowBackground(color c) on my object, then it’s going to assign the color variable.
But I can’t say :

color background_color = null;

I would like to use null in Java like None variable in Python because then in the display function of my object, I need to check if the variable background color has been assigned earlier.

Somebody have an idea?

Java’s 8 primitive datatypes can’t have null assigned to them: :zero:

If you really wanna go ahead w/ that, you may use Integer instead at your own risk: :warning:


The thing with color is that it’s not an object, but actually an integer, and the color name exists only in the Processing code. If you compile your application, then look into the folder of the result, then “sources”, then open the .java file, you’ll find that all color variables got changed to int.

As a funky but simple workaround, you could create yourself a class colorClass with 1 variable, and use that where needed.

class colorClass{
  color c;
  colorClass(color inColor){
    this.c = inColor;

This way, you could make yourself a colorClass background_color and then set it to new colorClass(coolColor) or null.

Or, you could store a separate Boolean value background_color_set and use it alongside.

1 Like

Yes it is a solution, thanks guys :+1:

As has already been said, primitives like int (color is an int) don’t support null. One option is an object wrapper – this is the difference between int and Integer in Java, for example, you could have color and Color.

Another option is to use the color alpha channel, and make black at 0% alpha a magic number for unassigned. This would mean that if a user set the background to black at 0% alpha (assuming that is even possible in your interface) they would be doing the equivalent of unsetting it / setting it to null.

final color NULLCOLOR = color(0, 0);

void setup() {
  color c = color(0, 0);
  c = color(255);

boolean isColorSet(color c) {
  if (c != NULLCOLOR) {
    return true;
  return false;