Jinput Gamepad buttons throwing exception and not recognized when triggered

Hi everyone, I have been trying to implement the Jinput library 2.0.9 in my robotics project and I have been having issues with polling my gamepad buttons. I use the Jinput getting started tutorial by

  • @author TheUzo007
  •     http://theuzo007.wordpress.com 
    

My gamepad controller is a Logitech F-310 wired to the computer
My HAT switch position works fine but when it comes to binding the buttons, I can never get the gamepad to work. I keep on getting this error when trying to bind actions to any of my gamepad buttons. It is as if it does not recognize any of my buttons and throws an exception every time I try to push a button.
**Exception in thread “main” java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:359)
at java.base/java.util.ArrayList.get(ArrayList.java:427)
at jinputjoysticktestv2.JInputJoystick.getButtonValue(JInputJoystick.java:243)

I use this piece of code to

		// Number of buttons.
		int numberOfButtons = joystick.getNumberOfButtons(); 

		//getNumberOfButtons();

		 
		// Button one on the controller.
		boolean  joystickButton_1 = joystick.getButtonValue(0);
		
		 if(numberOfButtons == 1){
			 
				System.out.println("Button 1 is pressed ");
				
				try {
					btrc.bluetoothStop();
				} catch (IOException e1) {
					System.out.println(e1.getMessage());
				}}

Can someone please help figure what I am doing wrong, I have been trying to solve the problem for months with no success.

Any help or advice would be much appreciated!

Cheers

Hi - welcome to jvm-gaming.org. I hope we can help figure this out.

I did a minor bit of formatting to your post. Please check that I didn’t make any mistakes in the process!

My naive first guess is to identify the collection involved and learn why it doesn’t have any space allocated for members. But I haven’t worked with controllers myself, so hopefully someone else more knowledgeable will pitch in.

@philfrei
thank you for the tweaking, and eveything lokks fine and as intended :wink:
I can only hope that someone can steer me in the right direction.

Cheers!

Is the fragment you display from the getButtonValue method listed at the bottom of the stack trace? If so, is line 243 displayed? Somewhere, there is an ArrayList involved that doesn’t have any members, it seems.

That’s just returning the number of buttons which is probably something like 14.

This code gets the status of button 1, however, the next if statement checks if there is only one button on the controller, which will not be true.
// Button one on the controller.
boolean joystickButton_1 = joystick.getButtonValue(0);

	 if(numberOfButtons == 1){

Try changing it to this:
// Button one on the controller.
boolean joystickButton_1 = joystick.getButtonValue(0);

	 if( joystickButton_1){

Also are you sure it’s actually picking up any input device at all?

1 Like

I was just now taking a closer look at the stack trace.
If the number of buttons is 0, attempting to get the 0th element will of course be out of bounds.
So, something probably wrong with how joystick is getting initialized.