question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NullPointerException when trying to use FlatSVGIcon

See original GitHub issue

NullPointerException Bug

Hi, I’m Clark… I use FlatLaf for their GUI Laf Themes for Java Desktop Applications, they are pretty cool and I am very happy with them, thank you for all your work around it, it’s awesome. So I just needed of course a way to use Icons that could be compatible with Light and Dark themes and always look good, so I found that apparently FlatLaf could use FlatSVGIcon in extras to use svg icon images, paint them and then generate an icon to use in our java applications, but I have not found any official guide or documentation by FormDev either in their website or elsewhere, it actually could be really helpful to have something like that to know exactly how it works and how to use that feature properly… anyway, I tried to look in the FlatLaf Demo app to maybe figure out how to do it, I saw how you set the icons, and I tried to do it that same way, but this bug happened, so I tried some other ways, tried to invoke maybe some method, but this bug always occurs… I don’t know why… so I am submitting this “bug” (actually I don’t know if it is a bug or I am just using it in a wrong way) to maybe report if it is not reported yet. I don’t know if it is due to maybe a change in the last version, or it is because it works well with JFormDesigner but it doesn’t work with the designer by default in Netbeans and/or Eclipse IDEs, I use Apache NetBeans IDE version 12.4 and Java jdk version 16.0.1 and I am using the version 1.6.1 of FlatLaf themes (both: core and intelliJ themes) and extras, I am using salamander as well given it is apparently a dependency to use the svg features.

Well, I hope this could be helpful to other people and maybe you all of course could know better than me what is happening and maybe tell us all a solution, or implement a fix, I don’t know, but hopefully all the people trying to use this feature, we all are be able to use it soon, because the project is really cool, I like it so much, thank you all for your time and your attention.

With love and enthusiasm, Clark.

Methods wrote to use FlatSVGIcon:

1_imethod_nvoke

2_method_invoked

Where:

  • mnuBuscar: It is a jMenuItem in a jMenuBar where I would like to set icons this way but with svgs: 3_mnuItems

  • admTicket: It is a Class where the important methods are stored, following the MVVM Design Pattern.

  • getIconoImagen: It is the method that I wrote for this task, it receives the svg image path as a String parameter and returns the icon ready to set it up in the menuItem, well that’s the idea at least.

  • “\search_white_18dp.svg”: This is the svg pathName, it is located in the project resources directory, so it has a relative location.

Exception StackTrace:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.net.URL.toURI()" because "url" is null
	at com.formdev.flatlaf.extras.FlatSVGIcon.update(FlatSVGIcon.java:337)
	at com.formdev.flatlaf.extras.FlatSVGIcon.getIconWidth(FlatSVGIcon.java:371)
	at com.formdev.flatlaf.ui.FlatMenuItemRenderer$MinSizeIcon.getIconWidth(FlatMenuItemRenderer.java:514)
	at java.desktop/javax.swing.SwingUtilities.layoutCompoundLabelImpl(SwingUtilities.java:1090)
	at java.desktop/javax.swing.SwingUtilities.layoutCompoundLabel(SwingUtilities.java:1003)
	at com.formdev.flatlaf.ui.FlatMenuItemRenderer.getPreferredMenuItemSize(FlatMenuItemRenderer.java:111)
	at com.formdev.flatlaf.ui.FlatMenuItemUI.getPreferredMenuItemSize(FlatMenuItemUI.java:86)
	at java.desktop/javax.swing.plaf.basic.BasicMenuItemUI.getPreferredSize(BasicMenuItemUI.java:504)
	at java.desktop/javax.swing.JComponent.getPreferredSize(JComponent.java:1680)
	at java.desktop/javax.swing.BoxLayout.checkRequests(BoxLayout.java:476)
	at java.desktop/javax.swing.BoxLayout.preferredLayoutSize(BoxLayout.java:293)
	at java.desktop/javax.swing.plaf.basic.DefaultMenuLayout.preferredLayoutSize(DefaultMenuLayout.java:71)
	at java.desktop/java.awt.Container.preferredSize(Container.java:1824)
	at java.desktop/java.awt.Container.getPreferredSize(Container.java:1808)
	at java.desktop/javax.swing.JComponent.getPreferredSize(JComponent.java:1682)
	at java.desktop/javax.swing.JMenu.getPopupMenuOrigin(JMenu.java:361)
	at java.desktop/javax.swing.JMenu.setPopupMenuVisible(JMenu.java:327)
	at java.desktop/javax.swing.JPopupMenu.menuSelectionChanged(JPopupMenu.java:1484)
	at java.desktop/javax.swing.MenuSelectionManager.setSelectedPath(MenuSelectionManager.java:134)
	at java.desktop/javax.swing.plaf.basic.BasicMenuUI.appendPath(BasicMenuUI.java:264)
	at java.desktop/javax.swing.plaf.basic.BasicMenuUI$Handler.mousePressed(BasicMenuUI.java:530)
	at java.desktop/javax.swing.plaf.basic.BasicMenuUI$MouseInputHandler.mousePressed(BasicMenuUI.java:390)
	at java.desktop/java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:288)
	at java.desktop/java.awt.Component.processMouseEvent(Component.java:6614)
	at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
	at java.desktop/java.awt.Component.processEvent(Component.java:6382)
	at java.desktop/java.awt.Container.processEvent(Container.java:2264)
	at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4993)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2322)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
	at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4934)
	at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4560)
	at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4504)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2308)
	at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2773)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
	at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
	at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
	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)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

3reactions
DevCharlycommented, Oct 28, 2021

…I have images either pngs, some other jpgs and svgs inside the Project System Resources folder located in <PROJECT_NAME>/src/main/resources …

perfect 👍

… try to use the path “/src/main/resources/search_white_18dp.svg” it still not working

That’s wrong. Do not pass /src/main/resources/ to FlatSVGIcon. This is just the project structure, but is not included in the built JAR.

If you have search_white_18dp.svg directly in resources then this new FlatSVGIcon( "search_white_18dp.svg" ) should work. If you have subfolders in resources then include them.

Take a look at the source code of the FlatLaf Demo: https://github.com/JFormDesigner/FlatLaf/tree/main/flatlaf-demo/src/main It also uses Gradle and has SVG icons in resources.

0reactions
ClarkCodescommented, Oct 31, 2021

…I have images either pngs, some other jpgs and svgs inside the Project System Resources folder located in <PROJECT_NAME>/src/main/resources …

perfect +1

… try to use the path “/src/main/resources/search_white_18dp.svg” it still not working

That’s wrong. Do not pass /src/main/resources/ to FlatSVGIcon. This is just the project structure, but is not included in the built JAR.

If you have search_white_18dp.svg directly in resources then this new FlatSVGIcon( "search_white_18dp.svg" ) should work. If you have subfolders in resources then include them.

Take a look at the source code of the FlatLaf Demo: https://github.com/JFormDesigner/FlatLaf/tree/main/flatlaf-demo/src/main It also uses Gradle and has SVG icons in resources.

Hi Karl,

I really want to thank you a lot, for your time and your attention to my question, I really appreciate it a lot.

I would like to tell you that finally my “issue” is solved (at the end it was more just a mere confusion or misunderstanding in my mind), and the answer was in your last reply, the solution was just removing the char / and given my svg images were in the project resources directory, just passing search_white_18dp.svg as parameter, this way: new FlatSVGIcon( "search_white_18dp.svg" ) would make it work, and now it just works right.

So at the end I am a little ashamed that although the answer was so easy to reach, I could not see it by myself, I was blinded because of the way to work of other mechanisms that I saw in the past, and also by my lack of knowledge about the FlatLaf project code, since that fully understand it would require a quite big amount of time being an external person.

So knowing how to manage this and also reading the issue post #402 to extract ideas from there (but using UIManager.getColor("MenuItem.foreground"); instead), I could figure out how make svg icons to look really nice and make them match with every Laf theme users choose, so the icons look like in these captures now:

preview_Arc Orange preview_Carbon preview_Cobalt 2 preview_Gruvbox Dark Hard preview_Solarized Light (Material) preview_Arc Dark Orange

Just really sweet and smooth, now I achieved what I was looking for.

So since this “issue” is now solved, ergo my purposes with this post, totally fulfilled, I would just await a couple of days for if somebody have not seen this and could want to see it and then I will close this issue thread.

Again, thank you very much to you and to all in the team involved that made this project possible, it makes some things a lot easier and supports a lot of us, Java Developers either we are learning or even working, with these tasks about Laf themes, apps customization and personalization settings that I think it’s really important in modern development today, thank you all very much.

This was Clark aka ClarkCodes, Cheers and… Happy Coding… 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - What is a NullPointerException, and how do I fix it?
The NullPointerException (NPE) typically occurs when you declare a variable but did not create an object and assign it to the variable before...
Read more >
How to Fix and Avoid NullPointerException in Java - Rollbar
NullPointerException in Java occurs when a variable is accessed which is not pointing to any object and refers to nothing or null.
Read more >
Question: How does FlatSVGIcon dark mode recoloring work ...
When I use a FlatSVGIcon with fill="#6E6E6E" (copied from the icons in the ... NullPointerException when trying to use FlatSVGIcon #400.
Read more >
Java NullPointerException - Detect, Fix, and Best Practices
NullPointerException is raised in an application when we are trying to do some operation on null where an object is required.
Read more >
Null Pointer Exception In Java - GeeksforGeeks
NullPointerException is thrown when program attempts to use an object reference that has the null value. These can be: Invoking a method from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found