# PImage in class

**URL:** <https://discourse.processing.org/t/pimage-in-class/11091>\
**Category:** Beginners\
**Created:** [May 10, 2019, 1:04am UTC](https://discourse.processing.org/t/pimage-in-class/11091 "2019-05-10T01:04:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jashuman19](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jashuman19](https://discourse.processing.org/u/jashuman19)\
**Post date:** [May 10, 2019, 1:04am UTC](https://discourse.processing.org/t/pimage-in-class/11091/1 "2019-05-10T01:04:19Z")

</div>

When I try to create a PImage instance variable in a class file, I get the following error:

```auto
Cannot find a class or type named “PImage”

```

Does anyone know why this is? For reference, the (simplified) class looks like this:

```auto
class MyObj {
  PImage image;
  
  MyObj(String imgName) {
    image = loadImage(imgName);
  }
}

```

The error occurs on line 2. I really appreciate the help. I just don’t get what the problem is here, since using PImage in my main file works fine.

---

<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:** [May 10, 2019, 1:15am UTC](https://discourse.processing.org/t/pimage-in-class/11091/2 "2019-05-10T01:15:29Z")

</div>

> [@jashuman19](#):
>
> … in a class file,

Is it a “.pde” or a “.java” file? ❔

---

<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:** [May 10, 2019, 1:27am UTC](https://discourse.processing.org/t/pimage-in-class/11091/3 "2019-05-10T01:27:52Z")

</div>

-a- i not recommend your naming a variable “image”  
as it is a function to draw a image ( as texture on a quad vertex)

-b-  
but basically the idea works here  
win 7 / 64bit / processing 3.5.3 JAVA mode

```auto
String infile = "data/moonwalk.jpg";

class MyImg {
  PImage img;
  MyImg(String imgName) {
     img = loadImage(imgName); 
  }
}

MyImg thepicture;

void setup() {
  size(640, 360);
  thepicture = new MyImg(infile);  
}

void draw() {
  image(thepicture.img,0,0);
}

```

---

<div class="post-metadata">

**Author:** ![jashuman19](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jashuman19](https://discourse.processing.org/u/jashuman19)\
**Post date:** [May 10, 2019, 2:01am UTC](https://discourse.processing.org/t/pimage-in-class/11091/4 "2019-05-10T02:01:35Z")

</div>

It is a “.java” file.

---

<div class="post-metadata">

**Author:** ![jashuman19](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jashuman19](https://discourse.processing.org/u/jashuman19)\
**Post date:** [May 10, 2019, 2:06am UTC](https://discourse.processing.org/t/pimage-in-class/11091/5 "2019-05-10T02:06:21Z")

</div>

a. Understood. I can change the variable name, but I’m pretty sure that isn’t creating the problem here.

b. Are you just suggesting that I put the MyObj (or MyImg) class in the same file as my setup and draw methods? I could do this, but it seems fairly inconvenient. Will it never work if it’s a seperate “.java” file?

Also for the record, I am on OSX Mojave, with Processing 3.4. I also quickly tested 3.5.3 and got the same error.

---

<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:** [May 10, 2019, 2:08am UTC](https://discourse.processing.org/t/pimage-in-class/11091/6 "2019-05-10T02:08:57Z")

</div>

> [@jashuman19](#):
>
> It is a “.java” file.

So you need to import it: `import processing.core.PImage;`

> **[PImage](http://processing.github.io/processing-javadocs/core/processing/core/PImage.html)**

Also, prefer to ask for an already loaded PImage rather than making your `class` **loadImage()** by itself:

## MyObj.java:

```auto
import processing.core.PImage;

public class MyObj {
  PImage img;

  public MyObj(final PImage pic) {
    img = pic;
  }
}

```

---

<div class="post-metadata">

**Author:** ![jashuman19](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jashuman19](https://discourse.processing.org/u/jashuman19)\
**Post date:** [May 10, 2019, 2:14am UTC](https://discourse.processing.org/t/pimage-in-class/11091/7 "2019-05-10T02:14:37Z")

</div>

Great. Thank you so much!

---

<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:** [May 10, 2019, 2:25am UTC](https://discourse.processing.org/t/pimage-in-class/11091/8 "2019-05-10T02:25:45Z")

</div>

> [@jashuman19](#):
>
> b. Are you just suggesting that I put the MyObj (or MyImg) class in the same file as my setup and draw methods? I could do this, but it seems fairly inconvenient.

the usual Processing way would be open a other TAB “classtab” in the IDE ( what would create a other file “classtab.pde”
