I've been trying to add to the Y coordinate after randomizing the X coordinate of some lines. It's giving me this error:
Exception in thread "Thread-0" java.lang.NullPointerException
at uy.com.vincent.map.Background.tick(Background.java:24)
at uy.com.vincent.Game.tick(Game.java:40)
at uy.com.vincent.Game.run(Game.java:90)
at java.lang.Thread.run(Unknown Source)
Code:
private int lineX[] = new int[50];
private int lineY[] = new int[50];
private Game game;
public Background(Game game) {
for(int i = 0; i < lineX.length; i++) {
lineX[i] = getRandom(game.getCanvas().getWidth());
lineY[i] = 0;
}
}
public void tick() {
for(int i = 0; i < lineX.length; i++) {
if(lineY[i] >= game.getCanvas().getHeight() + 100) { // Line 24
lineX[i] = getRandom(game.getCanvas().getWidth());
lineY[i] = 0;
}
lineY[i] += 1;
}
}
public int getRandom(int max) {
Random random = new Random();
int generatedRandom = random.nextInt(max + 1);
return generatedRandom;
}
I'm sorry if the solution is obvious. I'm still learning.