pyjnius icon indicating copy to clipboard operation
pyjnius copied to clipboard

java.util.logging.Logger NullPointerException with pip install

Open jeb2112 opened this issue 2 years ago • 8 comments

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.

jeb2112 avatar May 27 '22 21:05 jeb2112

I'm facing a similar issue with sl4j, it looks like a serious bug..

eldaduzman avatar Sep 12 '22 16:09 eldaduzman

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.

cmacdonald avatar Sep 12 '22 16:09 cmacdonald

Thanks @cmacdonald ! too bad the exception capturing api is not in the documentation, could have saved a lot of time.

eldaduzman avatar Sep 13 '22 16:09 eldaduzman

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?

cmacdonald avatar Sep 14 '22 09:09 cmacdonald

I am treating this as a request to add something to the documentation explaining how to extract a Java stacktrace from a Python JavaException.

Julian-O avatar Oct 27 '23 03:10 Julian-O

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

cmacdonald avatar Oct 27 '23 11:10 cmacdonald

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.]

Julian-O avatar Oct 28 '23 00:10 Julian-O

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.

Julian-O avatar Oct 28 '23 01:10 Julian-O