softwarespartan / IB4m

Interactive Brokers API for Matlab
GNU General Public License v2.0
62 stars 21 forks source link

reqScannerSubscription() - java.lang.NullPointerException #107

Closed quebien90 closed 3 years ago

quebien90 commented 3 years ago

@softwarespartan, I believe to have noticed an issue with the request reqScannerSubscription. For most of the Scan Codes such as 'MOST_ACTIVE' I get results as expected. Meaning I get a valid ticker list in return for valid Scan Codes and an error message for Invalid Scan codes, which is good. However, for a few Scan Codes, such as HIGH_MOODY_RATING_ALL, I get a NullPointerException as listed at the bottom. I notice, you already fixed a similar bug in #15, but I could not find the dedicated line in your source code to fix this one by myself (assuming it is a bug after searching for solutions on my side). Also, after such an unexpected crash I am forced to restart the session and previously created EventListeners and handles could not be properly deleted. This leads to multiple callback executions. Is there a way to clean the listeners after such a crash? So far I found myself forced to restart Matlab. Thank you a lot in advance for helping me fix the NullPointerException and answering my question.

Kind regards, Kevin

Error for reqId 3e6. (Matlab Version 2020a, TWS Version 973, Gateway Build 978.2c): -IBKR error 3000000: 165 Historical Market Data Service query message:no items retrieved Exception in thread "Thread-30": java.lang.NullPointerException at java.util.concurrent.ConcurrentHashMap.putVal(ConcurrentHashMap.java:1011) at java.util.concurrent.ConcurrentHashMap.put(ConcurrentHashMap.java:1006) at com.tws.Handler.processEvent(Handler.java:453) at com.tws.Handler.scannerDataEnd(Handler.java:920) at com.ib.client.EDecoder.processScannerDataMsg(EDecoder.java:1305) at com.ib.client.EDecoder.processMsg(EDecoder.java:239) at com.ib.client.EReader.processMsgs(EReader.java:116) at com.ib.client.EClientSocket$MessageProcessingThread.run(EClientSocket.java:46)

Despair2000 commented 3 years ago

A “clear all” will get rid of all listeners.

quebien90 commented 3 years ago

Thanks for your reply. Yes, clear all gets rid of many things. Unfortunatelly, I often end up with the error code

-1 507 Bad Message Length null,

after getting the session instance / reconnecting to the Gateway. Specially, if I call clear all before closing the socket or deleting the session (which would be the case after a crash). Also, once I receive error 507, there seems to be no going back. The API Client stays connected according to the IB Gateway and all my attempts to reset the connection fail, except closing Matlab. Have you ever experienced this behaviour? However, I was hoping for a more specific way to find and destroy left over listeners. Maybe with the use of the following functions, but I am stuck ... event.hasListener(TWS.Events.getInstance, TWS.Events.MARKETDATA) session.handler.removeScannerDataListener(myListener)

quebien90 commented 3 years ago

Found a solution to get rid of all listeners with lost handlers and to restart a broken connection. I work with source object coupled listeners only: https://www.mathworks.com/help/matlab/matlab_oop/listener-lifecycle.html and delete the persistent instance TWS.Evennts whenever I encounter a serious problem: delete(TWS.Events.getInstance). This works well for me, as I can call this deletion in my app startup function. Also, this is rather useful, as matlab app designer does not invoke delete() when making changes and hitting the Editor's re-run button (and causing uncoupled listeners stacking up).

Long story short: Broken listeners --> fixed NullPointerException --> open issue ...

softwarespartan commented 3 years ago

Looks like TWS API called scanner data end but never called scanner data.

I've updated the scanner data end call with a defensive check on scanner data map which should avoid/fix this situation.

Updated Jar available. Also updated the TWS973 src code as well.

Let me know if issue persists.

quebien90 commented 3 years ago

Thank you! I will test it and keep you posted.

quebien90 commented 3 years ago

Yes, I confirm the issue is fixed, thank you very much again!