superBasem / javahidapi

Automatically exported from code.google.com/p/javahidapi
Other
0 stars 0 forks source link

NullPointerException in HIDManager.openByPath() and openById() #59

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. In 32-bit Linux, attempt to open a device when there are none plugged into 
the system. I tried both OpenJDK and Oracle JREs. The issue is not present on 
my 64-bit Windows machine.

The line:

for(HIDDeviceInfo d : devs)

throws a NullPointerException because listDevices() returns null.

I fixed the problem by changing to the source code:

public HIDDevice openById(int vendor_id, int product_id, String serial_number) 
throws IOException, HIDDeviceNotFoundException
{
    HIDDeviceInfo[] devs = listDevices();

    if(devs == null)
    {
        throw new HIDDeviceNotFoundException();
    }
    else
    {
        for(HIDDeviceInfo d : devs)
        {
            if(d.getVendor_id() == vendor_id && d.getProduct_id() == product_id && (serial_number == null || d.getSerial_number().equals(serial_number)))
                return d.open();
        }
        throw new HIDDeviceNotFoundException();
    }
}

Original issue reported on code.google.com by dmorr...@gmail.com on 23 Nov 2013 at 5:01

GoogleCodeExporter commented 8 years ago
The code above works for me.
64-bit Linux (Ubuntu 14.04)
Oracle JRE 1.80_05-b13

Also had a problem with NullPointerException. But the reason wasn't a 
non-plugged HID device.
My application was not able to access the hid device. Only root may read/write 
access usb devices under linux. A udev rule in /etc/udev/rules.d was the 
solution, and grants application running as user of the group "beef" read-write 
permissions.

SUBSYSTEM=="usb", ATTRS{idVendor}=="1234", ATTRS{idProduct}=="4321", 
GROUP="beef", MODE="0664"

Original comment by jenskrue...@web.de on 30 Jul 2014 at 6:25