为什么我的自定义字体数组会出现 ArrayIndexOutOfBoundsException?

Why am I getting ArrayIndexOutOfBoundsException with my custom font array?

我正在为 #towerjam 制作游戏,我需要渲染一些我制作的字体。这是我正在使用的代码。

public static void render(String msg, Graphics g, int x, int y) {
    msg.toUpperCase();
    char[] msgArray = msg.toCharArray();
    for (char c : msgArray) {
        System.out.println();
        g.drawImage(letters[c-65], x, y, Tile.DRAW_SIZE, Tile.DRAW_SIZE, null);
        x+=Tile.DRAW_SIZE;
    }
}

数组 letters 是从我的 spritesheet 裁剪的 BufferedImages 数组。现在它给了我一个 ArrayIndexOutOfBoundsException,我不知道为什么。

字符串在 Java 中是不可变的(就像在许多其他语言中一样),因此由于 msg.toUpperCase(); 无法编辑原始字符串,它会创建一个包含大写字符和 returns它.

如果您想将此返回的字符串存储在 msg 中,只需将其重新分配回 msg:

msg = msg.toUpperCase();