# PImage and img=null;

**URL:** https://discourse.processing.org/t/pimage-and-img-null/17187
**Category:** Coding Questions
**Created:** [January 19, 2020, 10:18pm UTC](https://discourse.processing.org/t/pimage-and-img-null/17187 "2020-01-19T22:18:18Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)
#### Post date: [January 19, 2020, 10:18pm UTC](https://discourse.processing.org/t/pimage-and-img-null/17187/1 "2020-01-19T22:18:18Z")

</div>

I cobbled this together from something @Chrisir once wrote. I only have one small question. What, if anything, does ` img=null;` do? It is at the end of the `setup()` block.

```auto
PShape globe;
PImage img;

void setup() {
  size (600, 600, P3D);
  
  img = loadImage("test.jpg");
  
  globe = createShape(SPHERE, 100);

  globe.setStroke(false);

  globe.setTexture(img);

  img=null; // what is this?
}

void draw() {
  
  background(0);

  translate(width/2, height/2); 
  
  rotateY(map(mouseX, 0, width, 0, TAU));
  rotateX(map(mouseY, 0, height, 0, TAU));

  shape(globe, 0, 0);
}

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [January 20, 2020, 3:14am UTC](https://discourse.processing.org/t/pimage-and-img-null/17187/2 "2020-01-20T03:14:56Z")

</div>

if you use a picture of notable size  
and try that code with or without that line  
you could check on the by your running code use memory.  
i do see here ( win 10 ) some small difference.

 ![memtest](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f79170485a9a7fa30a8fb1fbbd55f95bb39c30e0.png)

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 20, 2020, 7:09am UTC](https://discourse.processing.org/t/pimage-and-img-null/17187/3 "2020-01-20T07:09:45Z")

</div>

PImage is an object and by setting it null I make sure it has an initial value. But no image in it

But maybe it’s null automatically (its default) - I don’t know.

But sometimes I check if(img!=null) to control if the loading of the image was successful

For null see reference I guess

---

<div class="post-metadata">

### Author: ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)
#### Post date: [January 20, 2020, 7:55am UTC](https://discourse.processing.org/t/pimage-and-img-null/17187/4 "2020-01-20T07:55:41Z")

</div>

Thanks @Chrisir  
So, should `img=null;` come earlier in the block, before the `loadImage()`? Or doesn’t it matter?

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 20, 2020, 8:42am UTC](https://discourse.processing.org/t/pimage-and-img-null/17187/5 "2020-01-20T08:42:08Z")

</div>

Ah, I see.

Since I don’t need the image anymore after copying it to globe, I deleted it afterwards to make clear it’s not used anymore. And it saves a bit of memory

It could also be a local variable in setup

---

<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: [January 20, 2020, 9:06am UTC](https://discourse.processing.org/t/pimage-and-img-null/17187/6 "2020-01-20T09:06:41Z")

</div>

> [@Chrisir](#):
>
> It could also be a local variable in **setup()**.

Or even better, just pass the loaded PImage as an argument for the PShape: 💡  
`globe.setTexture(loadImage("test.jpg"));`

> [@Chrisir](#):
>
> And it saves a bit of memory.

That depends whether the PShape object keeps the passed PImage reference to itself. 🙄

---

<div class="post-metadata">

### Author: ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)
#### Post date: [January 20, 2020, 9:34am UTC](https://discourse.processing.org/t/pimage-and-img-null/17187/7 "2020-01-20T09:34:28Z")

</div>

Now I understand! @Chrisir Thank you.

@GoToLoop  
Is the idea to get rid of the `img` completely?  
Like this:

```auto
PShape globe;

void setup() {
  size (600, 600, P3D);

  globe = createShape(SPHERE, 100);

  globe.setStroke(false);

  globe.setTexture(loadImage("test.jpg"));
}

void draw() {
  background(0);
  
  translate(width/2, height/2); 

  rotateY(map(mouseX, 0, width, 0, TAU));
  rotateX(map(mouseY, 0, height, 0, TAU));

  shape(globe, 0, 0);
}

```

---

<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: [January 20, 2020, 9:37am UTC](https://discourse.processing.org/t/pimage-and-img-null/17187/8 "2020-01-20T09:37:06Z")

</div>

> [@paulstgeorge](#):
>
> Is the idea to get rid of the _img_ completely?

Yup! If some value is gonna be used in loco only, we don’t need to store it in some variable. 😀
