Hi ppeps(Broken english),
I'm working on a 2D Maze game. I need some help to store objects in my 2D array. I get a NullPointer on this one: this.currentMap[colsCount][rowsCount] = objects.get(col);. The main calls the LevelGenerator constructor.
public final class LevelGenerator extends JFrame {
private HashMap<String, ItemObject> objects = new HashMap<>();
private ItemObject[][] currentMap;
private int HEIGHT = 320;
private int WIDHT = 480;
public JFrame frame = null;
public Level currentLevel = null;
private final List<Level> levels
= new ArrayList<Level>() {
{
add(new Level001());
add(new Level002());
add(new Level003());
}
};
public LevelGenerator() {
// Vul de frame
//this.frame = frame;
// Vul de objecten lijst
//objects.put("B", new Bazooka());
objects.put("", new EmptyTile());
objects.put("W", new Wall());
this.currentLevel = levels.get(0);
this.Load();
}
/// Laad de map in
public void Load() {
int rowsCount = 0;
int colsCount = 0;
for (String[] row : this.currentLevel.map) {
for (String col : row) {
this.currentMap[colsCount][rowsCount] = objects.get(col);
colsCount += 1;
}
rowsCount += 1;
}
this.Start();
}
public void Start() {
this.frame.setSize(this.HEIGHT, this.WIDHT);
this.frame.setLayout(new GridLayout(15, 10));
this.frame.add(this.currentLevel);
this.frame.setResizable(false);
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.frame.setVisible(true);
}
}
Code of itemObject:
public class ItemObject {
public int x = 0;
public int y = 0;
public String image = "";
public void setImage(String image) {
this.image = image;
}
}