NXTConnector Subclass Problem

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

NXTConnector Subclass Problem

Postby win1for » Wed Mar 24, 2010 8:37 am

Hi Everyone,


I am trying to create a connection and subclass it so that i can use it for both Bluetooth and USB but am having a little problem Here are the codes:

superclass:


Code: Select all
public class Connection {

   // private data members
   protected NXTConnector connection;
   protected DataInputStream dataInput;
   protected DataOutputStream dataOutput;
   
   public void initialize(){
      
       this.connection = new NXTConnector();
      
          this.dataInput = null;
      this.dataOutput = null;
   }
   
   public Connection() {
      this.initialize();
   }

// other code here ....

}



subclass:

Code: Select all

public class BluetoothConnection extends Connection{
   
   public void initialize(){
            
      boolean connected = this.connection.connectTo("btspp://");
      
      // Connect to any NXT over Bluetooth
      if (!connected) {
         System.err.println("Failed to connect to any NXT");
         System.exit(1);
      }

      this.setDataInput(this.getConnection().getDataIn());
      this.setDataOutput(this.getConnection().getDataOut());
   }
   
   public BluetoothConnection() {
// superclass constructor called to initiate the NXTConnector
      super();   
      this.initialize();
   }
   



I always get the "Failed to connect to any NXT" message.

Is it not possible to subclass the NXTConnector class?

Need help.

thanks[/code]
win1for
Novice
 
Posts: 48
Joined: Tue Mar 02, 2010 11:22 pm

Postby gloomyandy » Wed Mar 24, 2010 9:17 am

Are you sure that the code you have posted is correct? I would expect this to throw a NullPointer exception... If the code is correct try adding print statements into both of your initialize methods, I think you may be surprised by what you see. As it stands at the moment the initialize() in your Connection class will not be called and the initialize() in your BluetoothConnection class will be called twice... Or at least it would if the first call did not throw a NullPointerException....

If you don't understand why this is case spend some time lookup up java virtual methods and in particular have a look at this blog entry...
http://codefeed.com/blog/?p=77


Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3893
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby win1for » Wed Mar 24, 2010 11:27 am

thanks for your reply.
The initialize() in the Connection class is called in the constructor of the superclass.
So super() in the constructor of the subclass must call the constructor of the superclass which already has the initialize() method. So I think it must work.
win1for
Novice
 
Posts: 48
Joined: Tue Mar 02, 2010 11:22 pm

Postby gloomyandy » Wed Mar 24, 2010 12:22 pm

Did you actually do what I suggested and add print statements, or bother to read the link I posted for you?

With Java all methods are virtual. So when your Connection constructor is called (via super) from the BluetoothConection constructor and makes a call to this.initialize() it will call the initialize() in your BluetoothConnection class not the one in Connection... That is how things work in Java, and is what is explained in the blog link I posted...
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3893
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff