pyjnius
pyjnius copied to clipboard
java.util.logging.Logger NullPointerException with pip install
I can't get pyjnius=1.4.1 java logger to work in python 3.8.13 after pip install pyjnius
. The java logger does work after a conda install. And, the logger does work via python for android in an android 7.0 device after pip install.
from jnius import autoclass
class Demo():
def __init__(self):
Log = autoclass('java.util.logging.Logger')
self.logger = Log.getLogger(type(self).__name__)
gives the NullPointerException in jnius_utils.pxi line 91. The string argument to the constructor is not a null, and I tried using string literals as well. In a related note, the jnius_utils.pxi file is missing from site_packages/jnius after pip install pyjnius
. But whether it is an empty file created by touch jnius_utils.pxi
, or the file is copied manually from here on github, still get the same NullPointerException.
I'm facing a similar issue with sl4j, it looks like a serious bug..
You can look into the exception like this:
from jnius import JavaException
try:
d = Demo()
except JavaException as ja:
print("\n\t at ".join(ja.stacktrace))
raise(ja)
which displays output as follows:
java.lang.NullPointerException
at java.logging/java.util.logging.Logger.demandLogger(Logger.java:650)
at java.logging/java.util.logging.Logger.getLogger(Logger.java:717)
at java.logging/java.util.logging.Logger.getLogger(Logger.java:701)
A quick search takes me to https://stackoverflow.com/questions/33996439/nullpointerexception-at-java-util-logging-logger-demandlogger which makes two possible suggestions - suppressing the caller identification with a Java cmd line argument (which can be set using Jnius - see https://pyjnius.readthedocs.io/en/stable/api.html#jvm-options-and-the-class-path) or wrapping the function in some Java.
Thanks @cmacdonald ! too bad the exception capturing api is not in the documentation, could have saved a lot of time.
I tried hard to update the Python level traceback with the Java exception stacktrace, but in Python (unlike in Java), it seems a traceback cannot be faked.
@tshirtman I would propose instead updating the JavaException string to include the Java stacktrace by default. Thoughts?
I am treating this as a request to add something to the documentation explaining how to extract a Java stacktrace from a Python JavaException.
I worked for a considerable time to add the Java stacktrace into the Python traceback object, just like how Java builds stacktraces when error occurs for remote method invocation. I did not succeed in simulating tracebacks (though others might!)
I think instead, we could more simply override the __str__()
(or __repr__()
?) in jnius.JavaException to provide the Java stacktrace. I think that would immediately be more useful.
Alternatively, I see that Python 3.11 has "notes": https://docs.python.org/3/tutorial/errors.html#tut-exception-notes
Putting the traceback in the __str__()
or __repr__()
doesn't seem like a great answer to me. It is going to astonish the developers who use it for the Exception name.
Creating a traceback
object (or duck-typing one) does seem the best approach, but I hear you that it is more difficult than it first appears.
I think the existing attribute that is only on a Python JavaException sounds like the best fallback, with appropriate documentation.
[This is just an opinion.]
Oooh! Time to move this discussion to #203 which is very similar.
To summarise:
- Logger call is giving NullPointerException? Support request. Off-topic here.
- Documentation should include how to get the stack-trace? THIS issue.
- There should be better ways to get the stack trace? #203.