public class Game {
public class Truck {
private float x,y;
public Truck() {}
public float getX() {return this.x;}
public float getY() {return this.y;}
}
public class Fort {
public Fort() {
float x = truck.getX();
float y = truck.getY();
}
}
public Truck truck = new Truck();
public Fort fort = new Fort();
}
Beginner Java programmer trying to make a game. Getting an error when I try to get truck's x and y values to use in Fort's class method. So is it possible to call a method from another child class?
This is the error I'm getting:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.kroy.game.ETFortress.getTruckDistance(ETFortress.java:178)
at com.kroy.game.ETFortress.<init>(ETFortress.java:60)
at com.kroy.game.KroyGame.create(KroyGame.java:45)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:151)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:128)
That is because you are creating classes within a class.
If you really are a beginner, learn the basics before trying to tackle a game. You will get there eventually, but figure out how all the pieces of code can work together first. Then you can make anything.
Good luck!