Array Index Out of Bounds error on PGraphics.text()

This is a Jython’s major shortcoming when dealing w/ Java’s overloaded methods & constructors: :coffee:

You see, method text() got 9 overloaded signatures: :see_no_evil:

At self.graphics.text(self.character[x][y], int(x * tileSize), int(y * tileSize), int(tileSize), int(tileSize)), you’re attempting to use text()'s 7th sig w/ 5 parameters:
text(str, x1, y1, x2, y2).

But I’m highly suspicious Jython actually invokes text()'s 4th sig instead, which also got 5 parameters:
text(chars, start, stop, x, y).

chars char: the alphanumeric symbols to be displayed
start int: array index at which to start writing characters
stop int: array index at which to stop writing characters

So it seems Jython is passing your self.character[x][y] argument as if it were a Java char[] array instead of an actual String! :prayer_beads:

Unfortunately, Jython doesn’t have any native feature to allow us to forcibly pick a particular overloaded sig of a Java method when its own auto choice fails. :crying_cat_face:

For such cases, our last recourse would be to rely on the Java’s reflection API as I did in the forum post I’ve linked above. :unamused:

But for your particular case, there’s a much easier workaround: Use Java’s own String datatype: :bulb:
Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html

from java.lang import String

def set(self, x, y, character):
    self.character[x][y] = String(character)
    self._update.append((x,y))
2 Likes