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

**URL:** <https://discourse.processing.org/t/array-index-out-of-bounds-error-on-pgraphics-text/10239>\
**Category:** Processing.py\
**Created:** [April 13, 2019, 3:28pm UTC](https://discourse.processing.org/t/array-index-out-of-bounds-error-on-pgraphics-text/10239 "2019-04-13T15:28:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JRH](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jrh/32/4598_2.png) [@JRH](https://discourse.processing.org/u/JRH)\
**Post date:** [April 13, 2019, 3:28pm UTC](https://discourse.processing.org/t/array-index-out-of-bounds-error-on-pgraphics-text/10239/1 "2019-04-13T15:28:16Z")

</div>

Hello; new here. 👋

I have no idea what part of this is breaking. I have tried to reduce this as best as I can to a minimal example.

I have a class defined in a separate tab which is meant to represent something like a basic terminal or console output screen. This class has its own PGraphics instance to which all the characters in the terminal should be written when an update is needed. Then the terminal should be output with a call to consoleRoot.show() in my main tab.

The line that causes my array index error is module.py:26. If I comment out this line, save, and run, there is no error. I have already tried replacing the arguments to text() with constants rather than looking up a value in a 2-dimensional list.

Main tab:

```auto
import random as rng
from module import Console

consoleRoot = None

def setup():
    global consoleRoot
    size(800, 600)
    consoleRoot = Console(100,100)
    frameRate(999)

def draw():
    global consoleRoot
    consoleRoot.update()
    consoleRoot.show()
    consoleRoot.set(rng.randint(0,99), rng.randint(0,99), rng.choice('abcdefghijklmnopqrstuvwxyz'))

```

module.py:

```auto
tileSize = 16

class Console:
    def __init__ (self, w, h):
        self.width = w
        self.height = h
        self.graphics = createGraphics(w * tileSize, h * tileSize)
        self.graphics.beginDraw()
        self.graphics.textAlign(CENTER, CENTER)
        self.graphics.textSize(12)
        self.graphics.background(0)
        self.graphics.endDraw()
        self.character = [[' ' for i in range(h)] for j in range(w)]
        self._update = []
    def set(self, x, y, character):
        self.character[x][y] = character
        self._update.append((x,y))
    def update(self):
        if len(self._update) == 0:
            return
        self.graphics.beginDraw()
        for i in self._update:
            x, y = i
            self.graphics.rect(x * tileSize, y * tileSize, tileSize - 1, tileSize - 1)
            self.graphics.fill(224)
            self.graphics.text(self.character[x][y], int(x * tileSize), int(y * tileSize), int(tileSize), int(tileSize))
            self.graphics.fill(0)
        self.graphics.endDraw()
        del self._update[:]
    def show(self):
        image(self.graphics, 0, 0)

```

EDIT: To be clear (because I removed a line somewhere and confused my line numbers), the line that causes the error is in module.py and reads:

```auto
self.graphics.text(self.character[x][y], int(x * tileSize), int(y * tileSize), int(tileSize), int(tileSize))

```

---

<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:** [April 13, 2019, 7:21pm UTC](https://discourse.processing.org/t/array-index-out-of-bounds-error-on-pgraphics-text/10239/2 "2019-04-13T19:21:21Z")

</div>

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

> [@Issue when porting an example sketch from the Hemesh library to Python mode](https://discourse.processing.org/t/issue-when-porting-an-example-sketch-from-the-hemesh-library-to-python-mode/8570/6):
>
> WB\_Render3D::drawTriangle() got 6 overloaded versions of it: And outta those 6, there are 2 w/ the 1 parameter signature that is a container: And 1 w/ 1 parameter that is a single WB\_Triangle: Method WB\_IsoSurface::getTriangles() returns a List\<WB\_Triangle\>: But if we call print type(triangles), we’ll find out its actual datatype is: \<type 'org.eclipse.collections.impl.list.mutable.FastList'\> [Eclipse.org/collections/javadoc/9.0.0/org/eclipse/collections/impl/list/mutable/FastLi…](http://Eclipse.org/collections/javadoc/9.0.0/org/eclipse/collections/impl/list/mutable/FastList.html)

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

> **[text() / Reference](https://processing.org/reference/text_.html)**
>
> Draws text to the screen. Displays the information specified in the first parameter on the screen in the position specified by the additional parameters. A default font will be used unless a font is s…

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! 📿

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. 😿

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. 😒

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

```python
from java.lang import String

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

```

---

<div class="post-metadata">

**Author:** ![JRH](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jrh/32/4598_2.png) [@JRH](https://discourse.processing.org/u/JRH)\
**Post date:** [April 13, 2019, 9:57pm UTC](https://discourse.processing.org/t/array-index-out-of-bounds-error-on-pgraphics-text/10239/3 "2019-04-13T21:57:19Z")

</div>

Thanks for explaining! I knew this had to be one of those “internal” problems with Processing.py.

Since you’ve pointed out that there is a `text(chars, start, stop, x ,y)` function that expects a couple of `int`s, I’ve simply turned those two arguments into `float`s to convince Jython to use the other signature. Now it works fine! 🙂

---

<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:** [April 13, 2019, 10:43pm UTC](https://discourse.processing.org/t/array-index-out-of-bounds-error-on-pgraphics-text/10239/4 "2019-04-13T22:43:15Z")

</div>

> [@JRH](#):
>
> I’ve simply turned those two arguments into `float`s to convince Jython to use the other signature.

Well-thought out workaround! If the 2nd and/or the 3rd passed arguments are floating values, the 7th signature kicks in, b/c the 4th sig demands `int` values for its index parameters: 👌

```python
size(100, 100)
noLoop()

pg = createGraphics(width, height)
s = 'QuickFox'

with pg.beginDraw():
    pg.textSize(020)
    pg.fill(0xffFFFF00)
    pg.text(s, 10, 10.0, width>>1, height>>1)

background(0xff0000FF)
image(pg, 0, 0)

```

Another workaround is to go w/ the 3rd sig if you don’t need the box constraint: 😳  
**text** (str, x, y)

Notice this overloaded mess doesn’t affect the “global canvas” PApplet::**text()**: 😮

> **[text() \\ Language (API)](https://py.processing.org/reference/text.html)**
>
> Python Mode for Processing extends the Processing Development Environment with the Python programming language.

Only when we’re using the PGraphics::**text()** returned from PApplet::**createGraphics()**: 🙄

> **[createGraphics() \\ Language (API)](https://py.processing.org/reference/createGraphics.html)**
>
> Python Mode for Processing extends the Processing Development Environment with the Python programming language.

---

<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:** [April 13, 2019, 11:13pm UTC](https://discourse.processing.org/t/array-index-out-of-bounds-error-on-pgraphics-text/10239/5 "2019-04-13T23:13:00Z")

</div>

Rather than using 2 separated lists to refer to the same thing, why not those 3 values in 1 list? 💡

```python
def show(self): image(self.pg, 0, 0)

def set(self, x, y, c): self.chars.append(x, y, c)

def update(self):
    if not len(self.chars): return

    s = tileSize
    t = s - 1

    with self.pg.beginDraw():
        for ch in self.chars:
            x, y, c = ch
            xs = x*s
            ys = y*s

            self.pg.fill(0)
            self.pg.rect(xs, ys, t, t)

            self.pg.fill(0340)
            self.pg.text(c, float(xs), ys, s, s)

```

---

<div class="post-metadata">

**Author:** ![JRH](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jrh/32/4598_2.png) [@JRH](https://discourse.processing.org/u/JRH)\
**Post date:** [April 13, 2019, 11:25pm UTC](https://discourse.processing.org/t/array-index-out-of-bounds-error-on-pgraphics-text/10239/6 "2019-04-13T23:25:09Z")

</div>

You mean combine the characters list with the \_update list? I’ve developed my sketch to the point now where I can “pan” or scroll through a console larger than my window (think like a traditional ASCII roguelike), so I’m not sure that would work out unless I add an entire screen’s worth of indices to that update list whenever the screen is panned.

Otherwise, that would definitely be the way to go. I’m also considering having other layers (hence `consoleRoot`, as I may have other consoles).
