Processing halts program before it even runs, how do I stop this?

There is an “error” in my code that processing thinks is an error but really isn’t. It refuses to run because of this.

Here is my code.

import java.io.*;
import java.lang.*;
import java.util.*;

ArrayList<Object> objects = new ArrayList<>();
Player player;

interface drawable {
  void _draw();
}

interface removeable {
  void _del();
}

class Player extends PhysicsObject implements drawable, removeable {
  
  public Player(Float x, Float y, Float xexpand, Float yexpand) {
    super(x, y, xexpand, yexpand);
    objects.add(this);
  }
  
  void update() {
    ;
  }
  
  void _draw() {
    rect(0,0,100,100);
  }
  
  void _del() {
    objects.remove(objects.indexOf(this));
  }
  
}

void setup() {
  size(500,500);
  player = new Player(0.0, 0.0, 100.0, 100.0);
}

void draw() {
  for (Object elem : objects) {
    if (elem instanceof drawable) {
      elem._draw();
    }
  }
}

I’ve removed some of the irrelevant stuff like what PhysicsObject is because I know it isn’t the cause of the error.

Essentially, the interface “drawable” allows the class “Player” to have the function “_draw”. However, the code iterates over every object in “objects” (aka just the player right now) and uses “elem” instead of “player” for calling the function. However, processing thinks this is an error despite elem being player because reasons. It won’t even try to run the code anymore - just immediately stop me from running it - so try and catch doesn’t work.

Does anyone have any ideas about potential fixes or workarounds?

Hi @JellyJam,

Why main?
In your code above setup is called once on init, hence your main also called once. As no draw is defined the draw loop will not be called each frame…

Cheers
— mnse

import java.io.*;
import java.lang.*;
import java.util.*;

ArrayList<Object> objects = new ArrayList<>();
Player player;

interface drawable {
  void _draw();
}

interface removeable {
  void _del();
}

class Player extends PhysicsObject implements drawable, removeable {
  
  public Player(Float x, Float y, Float xexpand, Float yexpand) {
    super(x, y, xexpand, yexpand);
    objects.add(this);
  }
  
  void update() {
    ;
  }
  
  void _draw() {
    rect(0,0,100,100);
  }
  
  void _del() {
    objects.remove(objects.indexOf(this));
  }
  
}

void setup() {
  size(500,500);
  player = new Player(0.0, 0.0, 100.0, 100.0);
}

void draw() {
  for (Object elem : objects) {
    if (elem instanceof drawable) {
      elem._draw();
    }
  }
}

Thank you for the suggestion. I’ve updated the code to fit your suggestions.

Unfortunately, the issue still stands.

Hi @JellyJam,

you can’t call _draw on Object as Object class not having such function, so you need to cast it at lease to drawable…

import java.io.*;
import java.lang.*;
import java.util.*;

ArrayList<Object> objects = new ArrayList<Object>();
Player player;

interface drawable {
  void _draw();
}

interface removeable {
  void _del();
}
class PhysicsObject {
  float x, y, xexpand, yexpand;
  public PhysicsObject(float x, float  y, float  xexpand, float  yexpand) {
    this.x=x;
    this.y=y;
    this.xexpand=xexpand;
    this.yexpand=yexpand;
  }
}

class Dummy {
  @Override
  public String toString () {
    return "Hey! I'm Dummy and don't have a drawable interface!";
  }
}

class Player extends PhysicsObject implements drawable, removeable {

  public Player(Float x, Float y, Float xexpand, Float yexpand) {
    super(x, y, xexpand, yexpand);
    objects.add(this);
  }

  void update() {
  }

  void _draw() {
    rect(0, 0, 100, 100);
  }

  void _del() {
    objects.remove(objects.indexOf(this));
  }
}

void setup() {
  size(500, 500);
  player = new Player(0.0, 0.0, 100.0, 100.0);
  objects.add(new Dummy());

}

void draw() {
  background(0);
  for (Object elem : objects) {
    if (elem instanceof drawable) {
      ((drawable)elem)._draw();
    }
    else {
       text(""+elem,20,120); 
    }    
  }
}

… but instanceof isn’t the best practice at all. Maybe have a look here and here