Page 1 of 1

Java exception related to Logger?

Posted: Thu Sep 02, 2021 12:33 pm
by schumi00
When I initiate a model from a model file path (e.g. Model(filePathToModelFile)), Matlab throws a Java exception as below:

Java exception occurred:
java.lang.NullPointerException: null upcall object in OpenSim::LogSink::sinkImpl

at org.opensim.modeling.opensimSimulationJNI.new_Model__SWIG_1(Native Method)

at org.opensim.modeling.Model.<init>(Model.java:821)


What does this mean? It only happens when looping a few times. In Matlab debug mode, even if Model(filePathToModelFile) does not work, initiating an empty model (e.g. Model()) still works fine.

Is this related to sending the OpenSim messages to the Matlab Command Window?
At the beginning of the Matlab script, the "Logger.addSink(JavaLogSink())" is used to send OpenSim messages to the Matlab.

Re: Java exception related to Logger?

Posted: Thu Sep 02, 2021 1:24 pm
by aymanh
Hello Xiao,

Yes this is related to passing log messages from the OpenSim API to the log file (sink using the logger's terminology).
Generally the call to add sink should be done once at the start or one place where logging is needed and not inside a loop. My guess is that by creating the sink at the call site as described (Logger.addSink(JavaLogSink()) it's possible that the Java garbage collection scheme thinks it's not used downstream and deletes it causing the crash.

I'd suggest you either not add a sink altogether or add it once outside any looping. Something like

Code: Select all

myLog = JavaLogSink(); Logger.addSink(myLog)
so you have a live handle to the log and it doesn't get deleted by garbage collection. If you remove it entirely you may get no logging at all or the default clutter depending on your environment/Matlab version.

Please let us know if that helps,
-Ayman

Re: Java exception related to Logger?

Posted: Thu Sep 02, 2021 1:58 pm
by schumi00
Thanks Ayman for the quick reply.

To clarify: the Logger.addSink(JavaLogSink()) was originally called only once at the beginning of the script, not in the loop. However, when RRATool was called in a loop to run multiple times, Matlab threw a Java exception as described in the original post.

With some quick testing, the method that Ayman suggested seems to be more stable. No exception has been thrown by Matlab yet. If

Code: Select all

myLog = JavaLogSink(); Logger.addSink(myLog)
is more stable than

Code: Select all

the Logger.addSink(JavaLogSink())
, maybe the suggestion about "how to show OpenSim's log messages in the Matlab Command Window" on the Scripting with Matlab page https://simtk-confluence.stanford.edu/d ... mandWindow need to be updated?

Re: Java exception related to Logger?

Posted: Mon Sep 06, 2021 8:40 am
by aymanh
Good idea Xiao,

I went ahead and updated the page. Thanks for reporting.

-Ayman