I am trying to keep my code easy to manage and therefore I am trying to pass an instantiated DifferentialPilot class to a seperate class file (trying to instanciate that class file with the same pilot). I'm doing this just so when I change the values in the pilot I only need to do it once. When I pass the pilot, and try and use it I get a null pointer exception (16). My code is as follows;
In the table_map class;
- Code: Select all
public static DifferentialPilot pilot = new DifferentialPilot(1.4f, 6.485f, Motor.B, Motor.A);
public static void main(String[] args) throws Exception {
table_map tm = new table_map();
tm.map();
table_nav tn = new table_nav(xTacho, yTacho, xcm, ycm, pilot);
tn.nav();
}
and in the table_nav class;
- Code: Select all
DifferentialPilot pilot;
double xTacho;
double yTacho;
double xcm;
double ycm;
//Constructor for passed variables
public table_nav(double xTacho, double yTacho, double xcm, double ycm, DifferentialPilot pilot) {
xTacho = this.xTacho;
yTacho = this.yTacho;
xcm = this.xcm;
ycm = this.ycm;
pilot = this.pilot;
}
public void nav() {
pilot.setTravelSpeed(4);
pilot.travel(4);
}
When passing a class which requires variables to instantiate do I need to add extra things in the constructor?
Thanks guys!
Phil
