# I don't understand why this doesn't work

**URL:** <https://discourse.processing.org/t/i-dont-understand-why-this-doesnt-work/14327>\
**Category:** Coding Questions\
**Created:** [September 30, 2019, 5:32pm UTC](https://discourse.processing.org/t/i-dont-understand-why-this-doesnt-work/14327 "2019-09-30T17:32:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ImDaGr3en](https://avatars.discourse-cdn.com/v4/letter/i/f0a364/32.png) [@ImDaGr3en](https://discourse.processing.org/u/ImDaGr3en)\
**Post date:** [September 30, 2019, 5:32pm UTC](https://discourse.processing.org/t/i-dont-understand-why-this-doesnt-work/14327/1 "2019-09-30T17:32:47Z")

</div>

I’m trying to make some functions that allow me to show some images in the window, so I made a class in which I put the PImage variables and a function that allows me to load the images, but it doesn’t actually work. (And yes, if you’re asking so, I’m trying to reproduce a kind of Pokémon game copy in Processing, and I’m completely aware this will be almost impossible). Here’s the source code of the two files:

gameLogic:

```auto
int pX=0, pY=0;

void setup() {

  tiles.imgLoader(tiles.pokeCenter, "pokeCenter");
  size(1080, 720);
  background(0);
}

void keyPressed() {

  switch(key) {
  case 'w':
    pY-=10;
    break;
  case 'a':
    pX-=10;
    break;
  case 's':
    pY+=10;
    break;
  case 'd':
    pX+=10;
    break;
  }
}

void draw() {

  background(0);
  for (int a=0; a<width; a+=10)
    for (int b=0; b<height; b+=10) image(grass, a, b, 10, 10);
  image(pokeCenter, pX, pY, 120, 120);
}

```

tilesLoader:

```auto
public class tiles {

  PImage pokeCenter;
  void imgLoader(PImage img, String fileName) {
    img=loadImage(fileName+".png");
  }
}

```

PS I know that it doesn’t do anything with a bit of sense, but it’s just a test for now, only to see if I can actually do what I wanted to do

---

<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:** [September 30, 2019, 6:33pm UTC](https://discourse.processing.org/t/i-dont-understand-why-this-doesnt-work/14327/2 "2019-09-30T18:33:22Z")

</div>

A couple of points before we look at what went wrong. By convention in Java all class names start with and uppercase letter so I suggest you change the class name from `tiles` to `Tiles` this makes it easier when reading the code to identify the classes. Second the call to `size(...)` method should be the first one in `setup` and the call to `backgorund` is superfluous because you use it in the `draw()` method. So change you code to

```auto
void setup() {
  size(1080, 720);
  tiles.imgLoader(tiles.pokeCenter, "pokeCenter");
}

```

No lets look at the problem here is your class with the name corrected

```auto
public class Tiles {

  PImage pokeCenter;

  void imgLoader(PImage img, String fileName) {
    img=loadImage(fileName+".png");
  }
}

```

It has just one method which you call with

```auto
  tiles.imgLoader(tiles.pokeCenter, "pokeCenter");

```

which will not work because `tiles` is the name pf a class not an actual object of that class so correcting all these gives us

```auto
int pX=0, pY=0;

Tiles tiles = new Tiles();

void setup() {
  size(1080, 720);
  tiles.imgLoader(tiles.pokeCenter, "pokeCenter");
}

void keyPressed() {
  switch(key) {
  case 'w':
    pY-=10;
    break;
  case 'a':
    pX-=10;
    break;
  case 's':
    pY+=10;
    break;
  case 'd':
    pX+=10;
    break;
  }
}

void draw() {

  background(0);
  for (int a=0; a<width; a+=10)
    for (int b=0; b<height; b+=10) image(grass, a, b, 10, 10);
  image(pokeCenter, pX, pY, 120, 120);
}

public class Tiles {

  PImage pokeCenter;
  
  void imgLoader(PImage img, String fileName) {
    img=loadImage(fileName+".png");
  }
}

```

But you still have problems because you are trying to say where to store the image i.e. in `pokeCenter` by specifying the variable name using a string which won’t work.

Consider the statement `PImage pokeCenter;` inside the Tiles class is is a declaration telling Java that we want memory space for an _object reference_ of type PImage. This object reference contains all sorts of information including the memory location of theactual pixel data etc but at the moment it references nothing so has the value `null`.

Now the statement  
`tiles.imgLoader(tiles.pokeCenter, "pokeCenter");`  
We are passing the object reference in the first parameter BUT Java will send a _copy_ of the object reference not the _original_ reference object. Now inside the `imgLoader` the copy is being used to load the image when the method ends the parameters are destroyed so you lose the copy reference object and the original is unchanged so `tiles.pokeCenter` is still `null`.

There are other errors for instance you have `image(grass, a, b, 10, 10);` but you have not declared a PImage variable called `grass`.

This is not the full answer to all the errors but it might get you started down the right track.

---

<div class="post-metadata">

**Author:** ![ImDaGr3en](https://avatars.discourse-cdn.com/v4/letter/i/f0a364/32.png) [@ImDaGr3en](https://discourse.processing.org/u/ImDaGr3en)\
**Post date:** [September 30, 2019, 7:40pm UTC](https://discourse.processing.org/t/i-dont-understand-why-this-doesnt-work/14327/4 "2019-09-30T19:40:46Z")

</div>

Maybe it’s me that I’m stupid: is it impossible to make a construct that loads a mass set of `PImage` class objects, passing as arguments of an ipothetical function the `PImage` object and the name of the image I want to create? Plz explain me if so because I’m not a Java expert but I still need for my project to load a huge quantity of files, so it would be very expensive to make a `loadImage(...)` line for each of them…

PS referring to the line `image(grass, a, b, 10, 10);` I want to clarify it was another test I did before and I forgot to delete, but I actually knew it would have caused problems.

---

<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:** [September 30, 2019, 10:00pm UTC](https://discourse.processing.org/t/i-dont-understand-why-this-doesnt-work/14327/5 "2019-09-30T22:00:09Z")

</div>

```auto
public static class Tiles {

  private static HashMap<String, PImage> store = new HashMap<String, PImage>();

  public static void storeImage(PApplet pa, String fname){
    PImage img = pa.loadImage(fname + ".png");
    if(img != null){
      store.put(fname, img);
    }
  }

  public static PImage getImage(String fname){
    return store.get(fname);
  }

}

```

Assuming you have an image file called “pokemon.png” then to store the image you use  
`Tiles.storeImage(this, "pokemon");`

and to retrieve the image

`my_pimage = Tiles.getImage("pokemon");`

I wrote this on my tablet so I have not been able to test it for syntax errors.
