# PImages change to null in separate class

**URL:** <https://discourse.processing.org/t/pimages-change-to-null-in-separate-class/8403>\
**Category:** Coding Questions\
**Created:** [February 14, 2019, 5:55pm UTC](https://discourse.processing.org/t/pimages-change-to-null-in-separate-class/8403 "2019-02-14T17:55:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kyle](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@Kyle](https://discourse.processing.org/u/Kyle)\
**Post date:** [February 14, 2019, 5:55pm UTC](https://discourse.processing.org/t/pimages-change-to-null-in-separate-class/8403/1 "2019-02-14T17:55:49Z")

</div>

I am using Processing core in Eclipse IDE, and I’m currently working on some code where I have many object classes inside of the main class. Recently I decided to move one of those classes (a class called bat) into a separate class file, and use the extends function to connect it with the main class. This way, keeping all the classes in a separate file makes the code much easier to navigate. However, when doing this, when my object class runs image(), it tells me that the image I put in it is null. After examining a little using the debug function, I found that when the main class creates a new bat, ALL PImages in the whole code globally change to null. How do I prevent this from happening, or work around this issue? Also, please inform me if what I’m doing right now is even possible.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [February 14, 2019, 6:02pm UTC](https://discourse.processing.org/t/pimages-change-to-null-in-separate-class/8403/2 "2019-02-14T18:02:44Z")

</div>

Can’t see your code but I suspect that you have duplicated the PImage variables from the main sketch class (extends PApplet) into the bat class but these are not being initialised.

---

<div class="post-metadata">

**Author:** ![Kyle](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@Kyle](https://discourse.processing.org/u/Kyle)\
**Post date:** [February 15, 2019, 2:57am UTC](https://discourse.processing.org/t/pimages-change-to-null-in-separate-class/8403/3 "2019-02-15T02:57:08Z")

</div>

The way I do it is that I have all of the PImages created in the main class, and then initialized with loadImage in setup(). The bat class does not contain any loadImage or creating new PImages so the only thing I can think of is that possibly the extends could be breaking the PImages. And as mentioned before, PImages change to null _globally_, not just in the bat class.  
Here’s some of the code (the whole code is super long) but this may be helpful:

```auto
public class redsquare extends PApplet {
	public PImage bat1, bat2;

```

then,

```auto
public void setup() {
	bat1 = loadImage("bat1.png");
	bat2 = loadImage("bat2.png");
}

```

and this is where I create new bat classes, in a void which gets run in setup:

```auto
batlist.add(new bat(1024, 256, 1));
batlist.add(new bat(704, 128, 1));
batlist.add(new bat(1216, 640, 1));
batlist.add(new bat(1344, 320, 1));

```

and finally, this is the part of the bat class with the images.

```auto
image(bat2, xlocation, ylocation);
image(bat1, xlocation, ylocation);

```

I don’t know if seeing this code helps (I don’t feel like plopping 1000 lines of code on here) but what I suspect is that maybe the PImages don’t know where to load from in the second class, though that doesn’t explain why the PImages COMPLETELY change to null.

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [February 16, 2019, 2:55am UTC](https://discourse.processing.org/t/pimages-change-to-null-in-separate-class/8403/4 "2019-02-16T02:55:11Z")

</div>

This code that you provided is helpful and enough.

You need to do this instead:

```java
public class Redsquare {

       PApplet p;
	public PImage bat1, bat2;

        //constructor
        Redsquare(PApplet parent){
            p=parent;
            ...
        }

```

Now you can use any function from Processing by calling it from your PApplet member field like this:

```java
//THIS next is a member function of your class above
//THIS next has no relationship whatsoever to the setup() function in a Processing sketch
//THIS is the setup function that belongs to Redsquare class
public void setup() {
	bat1 = p.loadImage("bat1.png");
	bat2 = p.loadImage("bat2.png");
}

```

Kf

---

<div class="post-metadata">

**Author:** ![Kyle](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@Kyle](https://discourse.processing.org/u/Kyle)\
**Post date:** [February 16, 2019, 9:25pm UTC](https://discourse.processing.org/t/pimages-change-to-null-in-separate-class/8403/5 "2019-02-16T21:25:45Z")

</div>

I don’t know if I understand you incorrectly, but I’ve changed all the code that way like so:

```auto
public class redsquare {
	PApplet p;
	redsquare (PApplet parent) {
		p = parent;
	}
	public PImage bat1, bat2;

```

and added p. before every PApplet method.  
But now in the bat class, the bat constructor which is

```auto
bat (int x, int y, int elocation) {
	this.xlocation = x;
	this.ylocation = y;
	this.elocation = elocation;
	ogx = xlocation;
	ogy = ylocation;
	}

```

now complains that “implicit superconstructor redsquare() is undefined. Must explicitly invoke another constructor”. Am I doing something wrong or is something missing, like a super() or something?  
Thanks for your patience.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [February 16, 2019, 9:48pm UTC](https://discourse.processing.org/t/pimages-change-to-null-in-separate-class/8403/6 "2019-02-16T21:48:15Z")

</div>

I think kfrajer has mixed up your classes. I suggest you try something like.

```auto
public class Redquare extends PApplet {
  public ArrayList<Bat> bats = new ArrayList<Bat>();
  
  void setup(){
    bats.add (new Bat(this, 1024, 256, 1, "bat1.png");
    bats.add (new Bat(this, 704, 128, 1, "bat2.png");
   ...
}

```

and in draw …

```auto
  for(Bat b : bats){
    b.render();
  } 

```

Then the Bat class

```auto
public class Bat {

  PApplet p;
  public PImage bat;
  int xlocation, ylocation, elocation;
  int ogx, ogy; // I have assumed these are attributes
  
  //constructor
  Bat(PApplet parent, int x, int y, int elocation, String file) {
    p = parent;
    this.xlocation = x;
    this.ylocation = y;
    this.elocation = elocation;
    ogx = xlocation;
    ogy = ylocation;
    bat = p.loadImage(file);
  }

  public void render(){
    p.pushMatrix();
    p.translate(xlocation, ylocation);
    p.image(bat, 0, 0);
    p.popMatrix();
  }
}

```

By the way it is a convention in Java that class names always start with a capital letter 🙂

---

<div class="post-metadata">

**Author:** ![Kyle](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@Kyle](https://discourse.processing.org/u/Kyle)\
**Post date:** [February 19, 2019, 3:28am UTC](https://discourse.processing.org/t/pimages-change-to-null-in-separate-class/8403/7 "2019-02-19T03:28:02Z")

</div>

I’ve added a PApplet into the bat class and put the loadImage inside of the constructor:

```auto
public class bat extends redsquare {
	PApplet p;
	bat (PApplet parent, int x, int y, int elocation) {
		p = parent;
		this.xlocation = x;
		this.ylocation = y;
		this.elocation = elocation;
		ogx = xlocation;
		ogy = ylocation;
		bat1 = p.loadImage("bat1.png");
		bat2 = p.loadImage("bat2.png");
	}

```

But there seems to be another problem: the PApplets also change to null. It’s not just PImages but I’ve found that every single processing related class (PApplet, font, image, etc.) changes to null, too. So right now when it runs the p.loadImage(), it tells me that p is null as well. Do I need to do anything else to stop that from happening? I’m wondering if it’s a conflict with how the PApplet functions in other classes, but I’m still in the dark about why this affects everything even in the redsquare class.

P.S. Yeah I get the capital letters thing but I’m too lazy to make it that way… 😛

---

<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:** [February 19, 2019, 4:11am UTC](https://discourse.processing.org/t/pimages-change-to-null-in-separate-class/8403/8 "2019-02-19T04:11:46Z")

</div>

- [Forum.Processing.org/two/discussion/20543/multiple-classes-not-working#Item\_1](http://Forum.Processing.org/two/discussion/20543/multiple-classes-not-working#Item_1)

---

<div class="post-metadata">

**Author:** ![Kyle](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@Kyle](https://discourse.processing.org/u/Kyle)\
**Post date:** [February 20, 2019, 12:02am UTC](https://discourse.processing.org/t/pimages-change-to-null-in-separate-class/8403/9 "2019-02-20T00:02:33Z")

</div>

Thanks! I made the image work by putting a this in the bat constructor for the PApplet instead of p. However, there’s another problem, somewhat unrelated to the original problem: oddly enough, it won’t let me create object classes in the bat class. I want to create a coin by adding one to an arraylist:

```auto
coinlist.add(new coin (xlocation, ylocation, 1, elocation));

```

but it doesn’t work. There is no error or anything, it’s just that nothing happens- nothing gets added to the arraylist. (just for a little background, a coin object is one that shows a coin on screen that you can pick up). I have tried making a method in the main class that gets called in the bat class, which runs that line of code, but it still doesn’t do anything. Despite this, the coins still seem to work, because if I make a coin somewhere else in the code, it makes a coin. It just doesn’t work when it’s run from the bat class.  
What should I do to fix this problem?

---

<div class="post-metadata">

**Author:** ![Kyle](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@Kyle](https://discourse.processing.org/u/Kyle)\
**Post date:** [February 27, 2019, 4:05am UTC](https://discourse.processing.org/t/pimages-change-to-null-in-separate-class/8403/10 "2019-02-27T04:05:49Z")

</div>

Never mind. I fixed it. But I have another issue now, which I opened a new discussion for (since this one is currently dead):  
[Files must be loaded inside setup() or after it has been called](https://discourse.processing.org/t/files-must-be-loaded-inside-setup-or-after-it-has-been-called/8751)
