JAR Export在getClass()上抛出Null指针异常.getResource()

时间:2014-07-29 19:51:27

标签: java swing maven javax.imageio getresource

我从Eclipse中的GitHub克隆了这个项目(要求Maven快速设置)。我可能会也可能不会调整它,但我想将其导出到JAR文件中。 https://github.com/eugener/oxbow

当我在src / test / java / org / oxbow / swingbits / dialog / table / filter / TableFilterTest.java中运行测试main方法时,它运行正常。

但是当我将项目导出到JAR文件并在我自己的JTable上使用它时,我得到一个空指针异常。它显然失去了对图像图标的跟踪,无法导入它们。

java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at org.oxbow.swingbits.table.filter.TableFilterColumnPopup.buildContent(TableFilterColumnPopup.java:173)
at org.oxbow.swingbits.popup.PopupWindow$1.popupMenuWillBecomeVisible(PopupWindow.java:67)
at javax.swing.JPopupMenu.firePopupMenuWillBecomeVisible(Unknown Source)
at javax.swing.JPopupMenu.setVisible(Unknown Source)
at javax.swing.JPopupMenu.show(Unknown Source)
at org.oxbow.swingbits.popup.PopupWindow.show(PopupWindow.java:110)
at org.oxbow.swingbits.table.filter.TableFilterColumnPopup.showFilterPopup(TableFilterColumnPopup.java:278)
at org.oxbow.swingbits.table.filter.TableFilterColumnPopup.mouseReleased(TableFilterColumnPopup.java:237)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)

除了组织项目的设置之外,我对Maven了解不多。有人可以告诉我,如果我错过了一步吗?

1 个答案:

答案 0 :(得分:0)

我想出了这个问题,虽然确实需要修改代码。资源路径必须在JAR文件的上下文中是绝对的

new ImageIcon(getClass().getResource("/resources/org/oxbow/swingbits/table/filter/funnel_delete.png")))

而不是

new ImageIcon(getClass().getResource("funnel_delete.png")))