我有一个在 Eclipse 中运行良好的 Java 项目,但是当我将它导出到可运行的 Jar 文件(选择“将所需的库打包到 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 中完美运行。
在此先感谢好心人。