问题
This question is a possible duplicate of this and this thread. But since none of them have provided a clear solution for my problem, I'm asking it again.
My required task is to connect 2 Keyboards via USB and then manage the inputs of each Keyboard separately through a Java Application. This requirement has been answered into some progress in the first thread I have mentioned above by @nan but his solution did not work accurately for me. You can find his blog post on his solution for this here. He has done the solution using the java-hid-api and it seems accurate and working.
However the point I am stuck in it is at opening the HID Device. When I try to open the Device using the HIDManager
using either the openByPath()
or the openById()
methods it returns only null and therefore it throws the NullPointerException
. it is the same when I try it using the open()
method of HIDDeviceInfo
. this issue is listed in the java-hid-api page too but so far no one seems to have provided a working solution.
The OS I'm working on is Windows 7 32-bit
Here are all the 3 attempts I've made to open the HID Device
HIDDevice hidDevice = HIDManager.getInstance().openByPath(hidDeviceInfo.getPath());
HIDDevice hidDevice = HIDManager.getInstance().openById(hidDeviceInfo.getVendor_id(), hidDeviceInfo.getProduct_id(), hidDeviceInfo.getSerial_number());
HIDDevice hidDevice = hidDeviceInfo.open();
Has anyone been able to get through this problem? If so your help is highly appreciated.
Thank you!
Update 1: I just found out that this task could be accomplished with JInput, according to the answer provided by @paul-whelan in this thread. I am going to try it now but I'm stating here first seeking for any confirmation or a guide on how to do it if anyone already knows and have accomplished it.
回答1:
This is what you do to initialize the library:
ClassPathLibraryLoader.loadNativeHIDLibrary();
HIDManager hm=HIDManager.getInstance();
Then you can read the devices etc:
HIDDeviceInfo[] dvl = hm.listDevices();
for (HIDDeviceInfo dv:dvl){
System.out.println(dv.getManufacturer_string());
}
来源:https://stackoverflow.com/questions/21110930/nullpointerexception-when-opening-hid-device-using-java-hid-api-managing-inputs