FlatLaf icon indicating copy to clipboard operation
FlatLaf copied to clipboard

Random JProgressBar exceptions

Open Kokecena opened this issue 1 year ago • 2 comments

Hello, I recently improved the SplashScreen of my application by injecting it in some parts of my application configurations, it rarely throws these 2 exceptions

Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException: Cannot read field “width” because “d” is null

or

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "javax.swing.JProgressBar.isIndeterminate()" because "this.progressBar" is null

At first I thought it was due to not running within the EDT but it still rarely throws the exception, at first I thought it was my implementation, but I tried without placing a flatlaf theme and the error from the more than 100 times I ran the program did not throw the error again

This is the only thing I do in some configurations, image

and this is how I update the progress, Previously the code was only

SwingUtilities.invokeLater(()->{
    ...
    ...
});

and it kept throwing the same error

image

It doesn't really affect anything, it just throws that exception....

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "javax.swing.JProgressBar.isIndeterminate()" because "this.progressBar" is null
	at com.formdev.flatlaf.ui.FlatProgressBarUI.paint(FlatProgressBarUI.java:229)
	at com.formdev.flatlaf.ui.FlatProgressBarUI.update(FlatProgressBarUI.java:201)
	at java.desktop/javax.swing.JComponent.paintComponent(JComponent.java:842)
	at java.desktop/javax.swing.JComponent.paint(JComponent.java:1119)
	at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:952)
	at java.desktop/javax.swing.JComponent.paint(JComponent.java:1128)
	at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:952)
	at java.desktop/javax.swing.JComponent.paint(JComponent.java:1128)
	at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5311)
	at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedFPScales(RepaintManager.java:1721)
	at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1630)
	at java.desktop/javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1570)
	at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1337)
	at java.desktop/javax.swing.JComponent._paintImmediately(JComponent.java:5259)
	at java.desktop/javax.swing.JComponent.paintImmediately(JComponent.java:5069)
	at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:879)
	at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:862)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
	at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:862)
	at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:835)
	at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:784)
	at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1898)
	at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:771)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:741)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

I don't have the stack for the other error because it hasn't appeared, but a quick search on Google turned up this issue from another project saying that the error was from FlatLaf

I am using FlatLaf 3.4.1

OS

  • Windows 10
  • Windows 11

Java versions

  • Java 17 - Liberica
  • Java 17 - OpenJDK

Kokecena avatar Apr 01 '24 20:04 Kokecena

I don't have the stack for the other error because it hasn't appeared, but a quick search on Google turned up this issue from another project saying that the error was from FlatLaf

Did you post the right link? I can not find any reference to FlatLaf on that page.

Looks like a threading issue. At line 229 in FlatProgressBarUI.java it throws the NPE because "this.progressBar" is null: https://github.com/JFormDesigner/FlatLaf/blob/bde25f6ac8fa711af86f7bc12ee948071ee1a43a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatProgressBarUI.java#L229

A few lines before, "this.progressBar" is also used, but did not throw a NPE: https://github.com/JFormDesigner/FlatLaf/blob/bde25f6ac8fa711af86f7bc12ee948071ee1a43a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatProgressBarUI.java#L224

So the field was set to null in another thread. Probably because BasicProgressBarUI.uninstallUI() was invoked. This can occur e.g. if you destroy/dispose the progress bar not on EDT. Or maybe if invoke use SwingUtilities.updateComponentTreeUI() or FlatLaf.updateUI() not on EDT.

I would recommend to set a breakpoint at BasicProgressBarUI.uninstallUI() to find out where "this.progressBar" is set to null.

DevCharly avatar Apr 02 '24 13:04 DevCharly

I've added some logs (including stack trace) to FlatProgressBarUI that warns when modifying indeterminate progress bar not on AWT thread.

Please try latest 3.5-SNAPSHOT: https://github.com/JFormDesigner/FlatLaf#snapshots

DevCharly avatar May 09 '24 09:05 DevCharly

I had forgotten that I made this issue, an apology for not responding, the work had me busy haha, I will be doing tests

Kokecena avatar May 12 '24 20:05 Kokecena

Did you post the right link? I can not find any reference to FlatLaf on that page.

They make a small mention that it could come from FlatLaf, although I have never seen anything reported here, it is probably the typical threading issue.

image

Kokecena avatar May 12 '24 20:05 Kokecena

I've added some logs (including stack trace) to FlatProgressBarUI that warns when modifying indeterminate progress bar not on AWT thread.

Please try latest 3.5-SNAPSHOT: https://github.com/JFormDesigner/FlatLaf#snapshots

I have already done the tests and I see that the log is thrown, I close the window within the EDT and at the moment the exception has not been thrown, anyway, thank you for the message telling me that I should run it in the EDT sometimes it jumps, but it is very very strange , weird swing stuff haha, I'll close this issue

Kokecena avatar May 13 '24 19:05 Kokecena