简体   繁体   English

设置图标时java.lang.NullPointerException?

[英]java.lang.NullPointerException when setting icon?

I've recently tried to set my default application icon with a .ico file I made myself. 我最近尝试使用我自己创建的.ico文件设置我的默认应用程序图标。 This is the code I used to create the icon: 这是我用来创建图标的代码:

initComponents();
java.net.URL url = ClassLoader.getSystemResource("/src/calculatormedii/resources/CMedii.ico");
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.createImage(url);
getFrame().setIconImage(img);

I have no idea why it's so difficult to set the java desktop application icon in netbeans, it should just be a property field like the rest. 我不知道为什么在netbeans中设置java桌面应用程序图标这么困难,它应该只是一个属性字段,就像其余的一样。 When I try to run the program, these errors show up: 当我尝试运行程序时,会出现以下错误:

    Uncaught error fetching image:
java.lang.NullPointerException
    at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:99)
    at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:113)
    at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:240)
    at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
    at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)

No idea what to do now. 不知道现在该做什么。 If there is an alternate way to set the application icon please specify it (Like a property field in netbeans or something) 如果有另一种设置应用程序图标的方法,请指定它(如netbeans中的属性字段或其他内容)

I strongly suspect it's the URL you're using. 我强烈怀疑这是你正在使用的网址。 Using ClassLoader.getSystemResource , you shouldn't need a leading slash - but I very much doubt that you want the src part either. 使用ClassLoader.getSystemResource ,你不需要一个前导斜杠 - 但我非常怀疑你是否想要src部分。 You should make sure that your icon is copied to the same place that your class files is (eg a bin directory) and then just use: 您应确保将图标复制到类文件所在的位置(例如bin目录),然后使用:

URL url = ClassLoader.getSystemResource("calculatormedii/resources/CMedii.ico");

You shall print more of your stack trace. 您应该打印更多的堆栈跟踪。 If you look further down, you will find the caller line inside your code that leads to the exception in java librairies. 如果你向下看,你会在代码中找到导致java librairies中的异常的调用者行。

But in your case, the problem comes from the fact that the path you give to getSystemResource as a parameter should be relative to the folder containing the actual class file of the class you are coding in. 但在您的情况下,问题来自于您将getSystemResource作为参数提供的路径应该相对于包含您正在编码的类的实际类文件的文件夹。

More precisely, you can use both : 更确切地说,您可以同时使用:

  • ClassLoader.getSystemResource and then the path has to be relative to your classpath entries ClassLoader.getSystemResource然后路径必须相对于您的类路径条目
  • getClass.getResourceUrl and the the path has to be relative to the class file of class you are coding in. getClass.getResourceUrl和路径必须相对于您编码的类的类文件。
  • X.class.getResourceUrl and the the path has to be relative to the class file of class X. X.class.getResourceUrl和路径必须相对于类X的类文件。

If you use eclipse with maven, a default folder structure is to have 如果你将eclipse与maven一起使用,那么就应该有一个默认的文件夹结构

 /src/main/java
 /src/main/resources

And then everything is handled for you : copying the resources files inside the same folder as the java files are compiled to. 然后一切都为您处理:复制与编译java文件相同的文件夹内的资源文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM