# Null type with variables

**URL:** https://discourse.processing.org/t/null-type-with-variables/6782
**Category:** Beginners
**Created:** [December 20, 2018, 2:31pm UTC](https://discourse.processing.org/t/null-type-with-variables/6782 "2018-12-20T14:31:14Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [December 20, 2018, 2:31pm UTC](https://discourse.processing.org/t/null-type-with-variables/6782/1 "2018-12-20T14:31:14Z")

</div>

Hello there,

I want to do this :

I have a variable :

```auto
 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 :

```auto
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?

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [December 20, 2018, 2:42pm UTC](https://discourse.processing.org/t/null-type-with-variables/6782/2 "2018-12-20T14:42:32Z")

</div>

Java’s 8 primitive datatypes can’t have `null` assigned to them: 0️⃣  
[Docs.Oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html](http://Docs.Oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html)

If you really wanna go ahead w/ that, you may use Integer instead at your own risk: ⚠  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Integer.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Integer.html)

---

<div class="post-metadata">

### Author: ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)
#### Post date: [December 20, 2018, 8:10pm UTC](https://discourse.processing.org/t/null-type-with-variables/6782/3 "2018-12-20T20:10:08Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [December 21, 2018, 11:50am UTC](https://discourse.processing.org/t/null-type-with-variables/6782/4 "2018-12-21T11:50:46Z")

</div>

Yes it is a solution, thanks guys 👍

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [December 21, 2018, 5:26pm UTC](https://discourse.processing.org/t/null-type-with-variables/6782/5 "2018-12-21T17:26:33Z")

</div>

> [@josephh](#):
>
> I don’t want to assign it because it’s an object property. […] 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.

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.

```auto
final color NULLCOLOR = color(0, 0);

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

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

```

> false  
> true
