Passing instantiated Pilot class to another class.

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

Passing instantiated Pilot class to another class.

Postby philleonard » Mon Jun 25, 2012 5:18 pm

Hi there!

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
philleonard
New User
 
Posts: 3
Joined: Mon Jun 25, 2012 5:08 pm

Re: Passing instantiated Pilot class to another class.

Postby gloomyandy » Mon Jun 25, 2012 9:11 pm

Hi,
it looks like you do not fully understand the Java scoping rules and the use of the this variable. the statement...
pilot = this.pilot;
will be assigning the current value of the class member pilot (which will be null) to the local variable (which in this case is a parameter) called pilot. I suspect that what you really intended was...
this.pilot = pilot;
which will assign the current value of the local variable/parameter pilot to the class member called pilot. Note that you will also need to change the other statements that are similar to the above...

Andy

PS many people would consider it poor practice to use local variables/parameters that have the same names as class members, certainly if you used different names for the parameters you would have avoided this confusion...
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4239
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Passing instantiated Pilot class to another class.

Postby philleonard » Tue Jun 26, 2012 10:55 am

Thank you Andy :) Yes I ended up calling the variables a different name to keep things easier, but yes it seems I got confused with; pilot = this.pilot; and this.pilot = pilot; Thanks for clearing that up mate :)
philleonard
New User
 
Posts: 3
Joined: Mon Jun 25, 2012 5:08 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff