我有一个在Eclipse中运行良好的Java项目,但是当我将它导出到一个可运行的Jar文件时(选中“package required libraries into jar”)我得到以下错误:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:58)
Caused by: java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at cib.cad.kernel.cmds.StartGui.doCmd(StartGui.java:94)
at cib.util.cmd.CmdMgr.doCmd(CmdMgr.java:91)
at cib.cad.kernel.Kernel.begin(Kernel.java:1058)
at cib.cad.Stylemaster.main(Stylemaster.java:70)
... 5 more
Exception in thread "Thread-1" java.lang.NullPointerException
at cib.cad.kernel.Kernel.setPreferredLocation(Kernel.java:2615)
at cib.cad.view.ErrorMessagePrintStream$MsgThread._showMessageDialog(Err
orMessagePrintStream.java:303)
at cib.cad.view.ErrorMessagePrintStream$MsgThread.run(ErrorMessagePrintS
tream.java:268)
StartGui.doCmd(StartGui.java:94)中的代码如下所示:
try {
ClassLoader cl = StartGui.class.getClassLoader();
URL url = cl.getResource("cib/cad/img/StyleIcon.jpg");
Image icon = new ImageIcon(url).getImage(); //<-- Line 94
frame.setIconImage(icon);
}
catch (IllegalArgumentException e) {
}
我假设这个空指针异常正在引起,因为它找不到图标,但我不完全理解为什么它会在eclipse中完美运行。
先谢谢亲切的人。
答案 0 :(得分:0)
试试这个。
Image icon = new ImageIcon(StartGui.class.getClassLoader()
.getResourceAsStream("cib/cad/img/StyleIcon.jpg"))
.getImage();
希望这有帮助。