Thank you for your help!
My code was:
package com.Main.tile;
import com.Main.GamePanel;
import com.Main.UtilityTool;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class TileManager {
GamePanel gp;
public Tile[] tile;
public int mapTileNum[] [];
public TileManager(GamePanel gp){
this.gp = gp;
tile = new Tile[10];
mapTileNum = new int [gp.maxWorldCol] [gp.maxWorldRow];
getTileImage();
loadMap("/maps/world01.txt");
}
public void getTileImage(){
setup(0, "grass01", false);
setup(0, "wall", true);
setup(0, "water01", true);
setup(0, "earth", false);
setup(0, "tree", true);
setup(0, "sand", false);
}
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;
double screenX = worldX - gp.player.worldX + gp.player.screenX;
double 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, (int)screenX, (int)screenY, null);
}
worldCol++;
if(worldCol == gp.maxWorldCol){
worldCol = 0;
worldRow++;
}
}
}
}
and it always show the error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot read field "image" because "this.tile[tileNum]" is null
at com.Main.tile.TileManager.draw(TileManager.java:98)
at com.Main.GamePanel.paintComponent(GamePanel.java:165)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1119)
at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5311)
at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedImpl(RepaintManager.java:1657)
at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1632)
at java.desktop/javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1570)
at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1337)
at java.desktop/javax.swing.JComponent._paintImmediately(JComponent.java:5259)
at java.desktop/javax.swing.JComponent.paintImmediately(JComponent.java:5069)
at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:879)
at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:862)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:862)
at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:835)
at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:784)
at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1898)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:771)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:741)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)