从文件系统加载资源到Java应用程序时遇到的问题

huangapple go评论75阅读模式
英文:

Problems loading resource into java application from filesystem

问题

我正在尝试在NetBeans(适用于Mac)上在标签上设置图像图标。
我在项目文件夹中导入图像后设置了图像:
screenshot of selected image

当我尝试运行项目时,出现以下错误:

Exception in thread "main" java.lang.NullPointerException
at java.desktop/javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at Laboratorio.Login.initComponents(Login.java:52)
at Laboratorio.Login.<init>(Login.java:17)
at Laboratorio.Main.main(Main.java:26)

Login.java:52:

labelLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Laboratorio/Immagini/logo_550.png"))); 

图像的路径是:
/Users/salvatore/NetBeansProjects/Laboratorio/src/main/java/Laboratorio/Immagini/logo_550.png

有人能帮我吗?谢谢!

英文:

I'm trying to set an image icon on a label on NetBeans (for Mac).
I set the image after imported it in the project folder:
screenshot of selected image

When I try to run the project it fails with these errors:

Exception in thread "main" java.lang.NullPointerException
at java.desktop/javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at Laboratorio.Login.initComponents(Login.java:52)
at Laboratorio.Login.<init>(Login.java:17)
at Laboratorio.Main.main(Main.java:26)

Login.java:52:

labelLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Laboratorio/Immagini/logo_550.png"))); 

The path of the image is:
/Users/salvatore/NetBeansProjects/Laboratorio/src/main/java/Laboratorio/Immagini/logo_550.png

Can someone help me, please?
Thanks!

答案1

得分: 0

我解决了。
我之前使用Java和Gradle(从未使用过)... 我一直使用Java和Ant。现在它使用相同的代码正常工作。

英文:

I solved.
I was using Java with Gradle (never used)... I always used Java with Ant. Now it works correctly with the same code.

huangapple
  • 本文由 发表于 2020年8月12日 23:04:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63379377.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定