PImage = null ?

edited August 2017 in Questions about Code

I have a class Item with an attribute PImage image;

But i never create an instance of that image.

I do this for example:

class Heart extends Item{

  PImage h = loadImage("heart_pink.png");

  Heart(float x, float y){
    super(x,y);
  }

  void show() {
    image(h, x - h.width/2, y - h.height/2);
  }

And it doesnt work cuz on Item, on image i get a null pointer.

I fix it by saying: PImage image = createImage(0,0,0);

But i dont really like it since BEFORE i call that image, on heart, i loadImage.

Answers

  • https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    Rather than loading resources in classes, prefer passing an already loaded resource to the class: *-:)

    // Forum.Processing.org/two/discussion/23867/pimage-null#Item_1
    // 2017-Aug-19
    
    static final String FILENAME = "heart_pink.png";
    Heart heart;
    
    void setup() {
      heart = new Heart(width>>1, height>>1, loadImage(FILENAME));
    }
    
    class Item {
      float x, y;
    
      Item(float px, float py) {
        x = px;
        y = py;
      }
    }
    
    class Heart extends Item {
      PImage h;
    
      Heart(float x, float y, PImage img) {
        super(x, y);
        h = img;
      }
    }
    
  • And it doesnt work cuz on Item, on image i get a null pointer.

    interestingly, image h is only known in heart, not in item:

    // Forum.Processing.org/two/discussion/23867/pimage-null#Item_1
    // 2017-Aug-19
    
    static final String FILENAME = "heart_pink.png";
    Heart heart;
    Item i;
    
    void setup() {
      size(900, 900);
      i=new Item(2, 2); 
      heart = new Heart(width>>1, height>>1, loadImage(FILENAME));
    }
    
    void draw() {
      println(heart.h); println(i.h);
      heart.show();
    }
    
    // ======================================
    
    class Item {
    
      float x, y;
      PImage h;
    
      Item(float px, float py) {
        x = px;
        y = py;
      }
    
      void show() { 
        image(h, 
          x - h.width/2, 
          y - h.height/2);
      }
    }
    
    class Heart extends Item {
    
      Heart(float x, float y, 
        PImage img) {
    
        super(x, y);
        h = img.get();
      }
    }
    //
    
  • I have 2 classes (and i may add like 2 or 3 more) which extend from Item, so i CANT load an image on Item, cuz each class has its own image. I have a heart, spiders, i may have boomerangs and circles.

    So thats why i add heart_pink only on heart class.

  • edited August 2017

    This is my class Item (i need an image to so i can make the collide method):

    class Item{
    
      float x;
      float y;
      PImage image;
    
      Item(float x, float y){
        this.x = x;
        this.y = y;
        rect(x - image.width/2, y - image.height/2, image.width, image.height);
      }
    
      boolean collide(Item other){
      return (x < other.x + other.image.width &&
         x + this.image.width > other.x &&
         y < other.y + other.image.height &&
         this.image.height + y > other.y);
      }
    }
    

    This is the heart constructor:

    class Heart extends Item{
    
      PImage h = loadImage("heart_pink.png");
    
      Heart(float x, float y){
        super(x,y);
      }
    
      void show() {
        image(h, x - h.width/2, y - h.height/2);
      }
    

    And this is the Enemy constructor:

    class Enemy extends Item{
    
      float speed;
      PImage s = loadImage("spider.png");
    
      Enemy(float x, float y){
        super(x,y);
        speed = random(-4,-3);
      }
    

    As u see, each class take its own image before building the object. So i dont know why i get a null pointer on Item.

  • And sorry, idk how to write code format here

  • See link by gotoloop above

  • okey, done ^^

Sign In or Register to comment.