Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.util.logging.Logger NullPointerException with pip install #623

Open
jeb2112 opened this issue May 27, 2022 · 8 comments
Open

java.util.logging.Logger NullPointerException with pip install #623

jeb2112 opened this issue May 27, 2022 · 8 comments

Comments

@jeb2112
Copy link

jeb2112 commented May 27, 2022

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.

@eldaduzman
Copy link

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

@cmacdonald
Copy link
Contributor

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.

@eldaduzman
Copy link

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

@cmacdonald
Copy link
Contributor

cmacdonald commented Sep 14, 2022

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?

@Julian-O
Copy link
Contributor

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

@cmacdonald
Copy link
Contributor

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

@Julian-O
Copy link
Contributor

Julian-O commented Oct 28, 2023

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
Copy link
Contributor

Oooh! Time to move this discussion to #203 which is very similar.

To summarise:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants