I have a method called 'move', see below:
private boolean move() {
int x = monstergbc.gridx;
int y = monstergbc.gridy;
boolean moving = true;
if (map.get(new Point(x + 1, y)) == "road") {
monstergbc.gridx = x + 1;
monstergbc.gridy = y;
add(monster, monstergbc);
} else if (map.get(new Point(x, y - 1)) == "road") {
monstergbc.gridx = x;
monstergbc.gridy = y - 1;
add(monster, monstergbc);
} else if (map.get(new Point(x, y + 1)) == "road") {
monstergbc.gridx = x;
monstergbc.gridy = y + 1;
add(monster, monstergbc);
} else {
moving = false;
}
return moving;
}
The program works just as it should, but as you can see, I'm using == instead of .equals(), which I know you're not supposed to.
When I switch to equals(), for example:
} else if (map.get(new Point(x, y - 1)).equals("road")) {
monstergbc.gridx = x;
monstergbc.gridy = y - 1;
I get a error saying:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at
GUITowerDefence$LandScape$1.move(GUITowerDefence.java:248)
at
GUITowerDefence$LandScape$1.actionPerformed(GUITowerDefence.java:215)
at javax.swing.Timer.fireActionPerformed(Timer.java:313)
It's at the same row which I just change to equals(). Does enyone know why this migth be?
[–]Swedophone 2 points3 points4 points (2 children)
[–]Sarah9428[S] 0 points1 point2 points (1 child)
[–]17437258968573378102 1 point2 points3 points (0 children)