I am not quite sure why I am getting this error

package tile;

import java.awt.Graphics2D;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.imageio.ImageIO;

import main.GamePanel;
import main.UtilityTool;

public class TileManager {
    GamePanel gp;
    public Tile[] tile;
    public int mapTileNum[][];

    public TileManager(GamePanel gp) {

        this.gp = gp;

        tile = new Tile[42];
        mapTileNum = new int[gp.maxWorldCol][gp.maxWorldRow];

        getTileImage();
        loadMap("/maps/worldV2.txt");

    }
    public void getTileImage() {

            //Placeholder
            setup(0,"grass00", false);
            setup(1,"grass00", false);
            setup(2,"grass00", false);
            setup(3,"grass00", false);
            setup(4,"grass00", false);
            setup(5,"grass00", false);
            setup(6,"grass00", false);
            setup(7,"grass00", false);
            setup(8,"grass00", false);
            setup(9,"grass00", false); 
            setup(10,"grass00", false);
            setup(11,"grass01", false);
            setup(12,"water00", true);
            setup(13,"water01", true);
            setup(14,"water02", true);
            setup(15,"water03", true);
            setup(16,"water04", true);
            setup(17,"water05", true);
            setup(18,"water06", true);
            setup(19,"water07", true);
            setup(20,"water08", true);
            setup(21,"water09", true);
            setup(22,"water10", true);
            setup(23,"water11", true);
            setup(24,"water12", true);
            setup(25,"water13", true);
            setup(26,"road00", false);
            setup(27,"road01", false);
            setup(28,"road02", false);
            setup(29,"road03", false);
            setup(31,"road04", false);
            setup(32,"road05", false);
            setup(33,"road06", false);
            setup(34,"road07", false);
            setup(35,"road08", false);
            setup(36,"road09", false);
            setup(37,"road10", false);
            setup(38,"road11", false);
            setup(39,"road12", false);
            setup(40,"wall", true);
            setup(41,"tree", true);


    }
    public void setup( int index, String imageName, boolean collision) {

        UtilityTool uTool = new UtilityTool();

        try {
            tile[index] = new Tile();
            tile[index].image = ImageIO.read(getClass().getResourceAsStream("/tiles/" + imageName + ".png"));
            tile[index].image = uTool.scaleImage(tile[index].image, gp.tileSize, gp.tileSize);
            tile[index].collision = collision;
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public void loadMap(String filepath) {
        try {
            InputStream is = getClass().getResourceAsStream(filepath);
            BufferedReader br = new BufferedReader (new InputStreamReader(is));

            int col = 0;
            int row = 0;

            while (col <gp.maxWorldCol && row < gp.maxWorldRow) {
                String line = br.readLine();

                while (col < gp.maxWorldCol) {
                    String numbers[] = line.split(" ");

                    int num = Integer.parseInt(numbers[col]);

                    mapTileNum[col][row] = num;
                    col ++;
                }
                if (col == gp.maxWorldCol) {
                    col = 0;
                    row++;
                }
            }
            br.close();
        } catch (Exception e) {

        }
    }

    public void draw(Graphics2D g2) {

        int worldcol = 0;
        int worldrow = 0;


        while(worldcol < gp.maxWorldCol && worldrow < gp.maxWorldRow) {

            int tileNum = mapTileNum[worldcol][worldrow];

            int worldX = worldcol * gp.tileSize;
            int worldY = worldrow * gp.tileSize;
            int screenX = worldX - gp.player.worldX + gp.player.screenX;
            int screenY = worldY - gp.player.worldY + gp.player.screenY;
            if (worldX + gp.tileSize > gp.player.worldX - gp.player.screenX &&
                worldX - gp.tileSize < gp.player.worldX + gp.player.screenX &&
                worldY + gp.tileSize > gp.player.worldY -gp.player.screenY &&
                worldY - gp.tileSize < gp.player.worldY + gp.player.screenY) {
                g2.drawImage(tile[tileNum].image, screenX, screenY, null);
            }

            worldcol++;

            if(worldcol == gp.maxWorldCol) {
                worldcol =0;
                worldrow ++;

            }
        }

    }
}





public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D)g;

        tileM.draw(g2);

        for(int i = 0; i < obj.length; i++){
            if(obj[i] != null) {
                obj[i].draw(g2, this);
            }
        }

        player.draw(g2);

        g2.dispose();
    }

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot read field "image" because "this.tile[tileNum]" is null
at tile.TileManager.draw(TileManager.java:138)
at main.GamePanel.paintComponent(GamePanel.java:109)

Dani AI

Generated

The NullPointerException is coming from trying to use a Tile that was never initialized (tile[tileNum] is null). was on the right track — a missing or wrong setup call, or a map value outside the range of initialized tiles, is the usual culprit. The next steps are to detect which index is missing and to harden the loading code so this fails loudly instead of silently.

Quick diagnostics to add (run once after getTileImage or inside initialization):

for (int i = 0; i < tile.length; i++) {
    if (tile[i] == null) System.err.println("tile[" + i + "] is null");
}

Guard the draw path so it logs the offending coordinates and index if something is wrong (avoid crashing the whole paint cycle):

if (tileNum < 0 || tileNum >= tile.length || tile[tileNum] == null) {
    System.err.println("Invalid tile at col=" + worldcol + " row=" + worldrow + " tileNum=" + tileNum);
    continue; // or draw a placeholder sprite
}

Hardening loadMap and setup

  • Verify the resource stream is non-null before reading and throw/log a clear error if not found.
  • Split each line with line.trim().split("\\s+") (safer than a single space) and check tokens.length >= gp.maxWorldCol.
  • Parse ints inside a try/catch and validate each number is within 0..tile.length-1; log a descriptive error if not.
  • Replace empty catch blocks with logging or rethrow so initialization problems are visible instead of being silently ignored.

Other practical tips

  • Add a placeholder/default Tile in any null slot so rendering never touches a null pointer.
  • If image loading is heavy, finish loading before allowing painting (use an assetsLoaded flag or a SwingWorker) so paintComponent only runs once initialization completes.

These checks will reveal whether the issue is the one missing index (as suspected) or a bad value in the map file, and will make future problems much easier to find.

The code you posted looks ok. Maybe the problem is in the GamePanel Class?
Maybe it’s this scenario…
A JPanel class adds some children and that triggers a repaint on the Swing thread. But the child’s constructor is still running on the main thread so it’s possibly not complete when the repaint runs. This is most probable when the child constructor does any I/O.

.UPDATE.

ignore the above. You didn’t call setup for index 30!

Ps. The empty catch block when trying to read files is a really good way to make any problems totally undebuggable.

commented: Thank you so much +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.