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)