Joystick(int,int,int) has protected access

Java Help;

We are copying examples out of WPI Robotics Library User’s Guide.pdf which state that we may define a new Joystick(int USBPort#, int #ofAxis, int #ofButtons) but when we do so Netbeans gives us an error stating that

“Joystick(int,int,int) has protected access in edu.wpi.first.pwilibj.Joystick …”

Alt Ent shows hints which repeats the same message.

First what does this mean, and second how do we fix it. Our team wants to use a gamepad… And we were hoping to be able to determine what the gamepad response is tonight.

We did find Joystic.java and see that Joystick( int port, int numAxisTypes, int numButtonTypes){} is defined as protected. Can we just change this to public? Is this a bug? (note changing it to public and saving the file hasn’t worked yet.) (note again, quite Netbeans, restarted, and now have no error message). This is extremely frustrating as the documentation didn’t work the first time and I don’t know if what I did to fix it is correct, nor if it broke something else?

New questions;

  1. If we change a library function should we create a new copy of it and put it in our current project instead of using the library?
  2. Why didn’t Netbeans see the library change until we restarted it?
  3. How do we determine if our change doesn’t cause additional problems and break something else?

You can make your own class that extends Joystick, and then call the protected super class constructor in your derived class’ public constructor. Something like:


class MyJoystick extends Joystick
{
  public MyJoystick(int port)
  {
    super(port, NUM_AXES, NUM_BUTTONS);
  }
}

protected means you can only access it if your class derives from the class it is defined in,

We did find Joystic.java and see that Joystick( int port, int numAxisTypes, int numButtonTypes){} is defined as protected. Can we just change this to public? Is this a bug? (note changing it to public and saving the file hasn’t worked yet.) (note again, quite Netbeans, restarted, and now have no error message). This is extremely frustrating as the documentation didn’t work the first time and I don’t know if what I did to fix it is correct, nor if it broke something else?

When you change the library you have to rebuild it not just save it. When you first start netbeans it does a full build. You can change anything you want, but be careful:
When updates come out your changes are over-written.
Chances are the code that is there is better than what you will replace it with. That isn’t a poke at you, and there are bugs here and there, but in general the WPI guys do a good job.
I changed a few things here and there, including making things public. But be careful and prepared to maintain it yourself.

New questions;

  1. If we change a library function should we create a new copy of it and put it in our current project instead of using the library?

I would usually so that they aren’t over-written with updates.

  1. Why didn’t Netbeans see the library change until we restarted it?

Answered above.

  1. How do we determine if our change doesn’t cause additional problems and break something else?

That is indeed the question! :yikes:
If you aren’t comfortable that you are either fixing a definte bug, or creating a new functionality, you should leave it alone. You can always extend the Joystick or HID classes and not worry that you’ve caused an unforeseen side effect.
(BTW when you do find actual bugs post them to Source Forge so that they can get fixed for everyone)
Good Luck & happy Java-ing

Thanks guys;

I’ll look into how and why to extend a class. Never done that before. I assume I need more than Jared341’s example?

Being a newbe to JAVA and not a programmer I didn’t realize that super() calls the initiating function with the additional arguments. This probably won’t be clear to non-JAVA programmers either. So in addition to looking up Java extends class you have to look up the keyword super().

The reason why that constructor is protected is that you’re not supposed to use it if you are just making a Joystick object (which assumes you are using an Attack 3 joystick, so it sets up the axes and buttons according to that joystick). But if you are trying to make a Joystick object that is not for an Attack 3, then Jared’s suggestion would be the right thing to do. Make your own subclass of Joystick to represent your gamepad. In that case you be representing your own custom Joystick, so you would need to customize the axes and buttons.
Some more complete example code might be:


import edu.wpi.first.wpilibj.Joystick;

public class Gamepad extends Joystick {
  
  final static int NUM_AXES = 3; // these would be the default values for the
  final static int NUM_BUTTONS = 11; // Attack 3 joysticks included in the KOP
  
  Gamepad(int USBport) {
    super(USBport, NUM_AXES, NUM_BUTTONS); // initialize Joystick with the provided USB port
                                           // and the constants for number of buttons and axes
  }
  
}

Extending the Joystick class basically makes it so Gamepad inherits all the methods and variables that Joystick has (and allows you to access protected methods and variables), but you can add in more functionality.