jSystemThemeDetector icon indicating copy to clipboard operation
jSystemThemeDetector copied to clipboard

[MacOS Catalina] NPE: Couldn't execute theme name query with the Os

Open DrDaleks opened this issue 3 years ago • 15 comments

Hi, Just noticed this NPE, wondered if this is normal or an OS-related issue somehow. My code calls OsThemeDetector.getDetector().isDark() at some point, and the full stack trace reads

[AWT-EventQueue-0] ERROR com.jthemedetecor.MacOSThemeDetector - Couldn't execute theme name query with the Os
java.lang.NullPointerException
	at java.base/java.util.regex.Matcher.getTextLength(Matcher.java:1770)
	at java.base/java.util.regex.Matcher.reset(Matcher.java:416)
	at java.base/java.util.regex.Matcher.<init>(Matcher.java:253)
	at java.base/java.util.regex.Pattern.matcher(Pattern.java:1133)
	at com.jthemedetecor.MacOSThemeDetector.isDarkTheme(MacOSThemeDetector.java:96)
	at com.jthemedetecor.MacOSThemeDetector.isDark(MacOSThemeDetector.java:86)

More specifically, the NPE is due to the following call (within the isDark() method) returning the '0' byte:

Foundation.invoke(userDefaults, "objectForKey:", Foundation.nsString("AppleInterfaceStyle"))

I should mention that this happens during daytime (and luckily isDark() returns false), so no harm done (besides an avoidable(?) pollution of the logger)

Any clue where that might come from?

In case this helps: using version 3.7 @ openjdk11 @ macOS 10.15.7

DrDaleks avatar Oct 23 '21 11:10 DrDaleks

Got the same error on Monterey 12.1 image

rosamarco avatar Dec 15 '21 00:12 rosamarco

Also facing same error on Monterey 12.1

peavers avatar Dec 21 '21 19:12 peavers

+1 Big Sur, JDK 17 I wonder if the matcher pattern catches the case where system Auto theme is set

ctipper avatar Jan 07 '22 14:01 ctipper

Developer seems to have explored this issue:

For working with the new macOS Catalina you need to combine AppleInterfaceStyle with this new value introduced AppleInterfaceStyleSwitchesAutomatically

https://stackoverflow.com/a/57429660

with a reference to https://github.com/ruiaureliano/macOS-Appearance

with apologies for possible impertinence

ctipper avatar Jan 07 '22 21:01 ctipper

This library would be useful if this issue were fixed. With reference to the example project referenced above the developer quietly updated his project with the correct property for macOS > 10.15 NSApplication.shared.effectiveAppearance.debugDescription A potential PR would need to check platform version.

https://github.com/ruiaureliano/macOS-Appearance/../AppDelegate.swift#L50

ctipper avatar Apr 09 '22 09:04 ctipper

In the meantime, does anyone know how to silence the logger?

I don't fully understand how the logger works. I've tried using

java.util.logging.Logger.getLogger("com.jthemedetecor.MacOSThemeDetector").level = Level.OFF
	java.util.logging.Logger.getLogger("com.jthemedetecor").level = Level.OFF

in my code, but that does nothing.

Whenever I search "how to disable a org.slf4j.Logger" people are talking about things like log4j. It's very confusing. I don't think I have log4j installed, but just in case I tried adding a "log4j.properties" to my classpath with log4j.logger.com.jthemedetecor=OFF but that is not doing anything either.

mgroth0 avatar May 21 '22 23:05 mgroth0

It sounds like you don't want logging at all, in which case you should add sl4j-nop (no-op) logger to your dependencies. I only know log4j with that if you have a logging configuration (log4j2.xml) on your classpath it should ignore libraries that are not configured. This library uses logback with sl4j I have no knowledge of that but it shouldn't matter if you are not using it.

ctipper avatar May 22 '22 08:05 ctipper

But what if I just want to disable logging for jthemedetecor without disabling logging for other libraries?

mgroth0 avatar May 23 '22 05:05 mgroth0

You should try to configure logging and then you can selectively enable libraries, what you were doing is unlikely to work.

-- Sent with Spark On 23 May 2022, 06:10 +0100, Matt Groth @.***>, wrote:

But what if I just want to disable logging for jthemedetecor without disabling logging for other libraries? — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

ctipper avatar May 23 '22 12:05 ctipper

12.4 JDK18 same thing

nybbs2003 avatar Jul 03 '22 11:07 nybbs2003

In the meantime, does anyone know how to silence the logger?

I don't fully understand how the logger works. I've tried using

java.util.logging.Logger.getLogger("com.jthemedetecor.MacOSThemeDetector").level = Level.OFF
	java.util.logging.Logger.getLogger("com.jthemedetecor").level = Level.OFF

in my code, but that does nothing.

Whenever I search "how to disable a org.slf4j.Logger" people are talking about things like log4j. It's very confusing. I don't think I have log4j installed, but just in case I tried adding a "log4j.properties" to my classpath with log4j.logger.com.jthemedetecor=OFF but that is not doing anything either.

use the following two lines in log4j2.properties

logger.jthemedetecor.name = com.jthemedetecor
logger.jthemedetecor.level = off

reference

nybbs2003 avatar Jul 04 '22 04:07 nybbs2003

In the meantime, does anyone know how to silence the logger? I don't fully understand how the logger works. I've tried using

java.util.logging.Logger.getLogger("com.jthemedetecor.MacOSThemeDetector").level = Level.OFF
	java.util.logging.Logger.getLogger("com.jthemedetecor").level = Level.OFF

in my code, but that does nothing. Whenever I search "how to disable a org.slf4j.Logger" people are talking about things like log4j. It's very confusing. I don't think I have log4j installed, but just in case I tried adding a "log4j.properties" to my classpath with log4j.logger.com.jthemedetecor=OFF but that is not doing anything either.

use the following two lines in log4j2.properties

logger.jthemedetecor.name = com.jthemedetecor
logger.jthemedetecor.level = off

reference

In case anyone is using log4j2.xml style properties you can use this:

<Loggers>
    <Logger name="com.jthemedetecor" level="error">
    </Logger>
    <Root level="info">
      ...
    </Root>
</Loggers>

vewert avatar Jan 18 '23 22:01 vewert

This issue seems to be partially solved by #30, but the fix is currently unreleased.

Wandmalfarbe avatar Aug 12 '23 16:08 Wandmalfarbe

Having the same issue, any news on this? Using ventura


RROR: Couldn't execute theme name query with the Os: java.lang.NullPointerException: Cannot invoke "java.lang.CharSequence.length()" because "this.text" is null
	at java.base/java.util.regex.Matcher.getTextLength(Matcher.java:1808)
	at java.base/java.util.regex.Matcher.reset(Matcher.java:461)
	at java.base/java.util.regex.Matcher.<init>(Matcher.java:256)
	at java.base/java.util.regex.Pattern.matcher(Pattern.java:1180)
	at com.jthemedetector/com.jthemedetecor.MacOSThemeDetector.isDarkTheme(MacOSThemeDetector.java:96)
	at com.jthemedetector/com.jthemedetecor.MacOSThemeDetector.isDark(MacOSThemeDetector.java:86)
	at [email protected]/org.jabref.gui.theme.ThemeManager.updateThemeSettings(ThemeManager.java:81) 

Siedlerchr avatar Nov 10 '23 20:11 Siedlerchr

PSA for anyone here the project is hosted by jitpack.io from which you can get dependency from latest commit for example, not just the releases.

https://jitpack.io/#Dansoftowner/jSystemThemeDetector

ctipper avatar Nov 11 '23 12:11 ctipper