2015-02-10 29 views
0

我添加了一個形象我的jList元素,但是當我運行框架這將引發異常:圖標處理拋出NullPointerException

java.lang.NullPointerException 
at javax.swing.ImageIcon.<init>(ImageIcon.java:217) 
at org.openstreetmap.josm.plugins.laneconnector.LaneRelationsDialog.createImageMap(LaneRelationsDialog.java:527) 
at org.openstreetmap.josm.plugins.laneconnector.LaneRelationsDialog.<init>(LaneRelationsDialog.java:363) 
at org.openstreetmap.josm.plugins.laneconnector.LaneRelationsAction.actionPerformed(LaneRelationsAction.java:38) 
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1663) 
at javax.swing.JComponent.processKeyBinding(JComponent.java:2879) 
at javax.swing.KeyboardManager.fireBinding(KeyboardManager.java:307) 
at javax.swing.KeyboardManager.fireKeyboardAction(KeyboardManager.java:263) 
at javax.swing.JComponent.processKeyBindingsForAllComponents(JComponent.java:2971) 
at javax.swing.JComponent.processKeyBindings(JComponent.java:2963) 
at javax.swing.JComponent.processKeyEvent(JComponent.java:2842) 
at java.awt.Component.processEvent(Component.java:6304) 
at java.awt.Container.processEvent(Container.java:2234) 
at java.awt.Component.dispatchEventImpl(Component.java:4883) 
at java.awt.Container.dispatchEventImpl(Container.java:2292) 
at java.awt.Component.dispatchEvent(Component.java:4705) 
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954) 
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806) 
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074) 
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945) 
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771) 
at java.awt.Component.dispatchEventImpl(Component.java:4754) 
at java.awt.Container.dispatchEventImpl(Container.java:2292) 
at java.awt.Window.dispatchEventImpl(Window.java:2739) 
at java.awt.Component.dispatchEvent(Component.java:4705) 
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746) 
at java.awt.EventQueue.access$400(EventQueue.java:97) 
at java.awt.EventQueue$3.run(EventQueue.java:697) 
at java.awt.EventQueue$3.run(EventQueue.java:691) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86) 
at java.awt.EventQueue$4.run(EventQueue.java:719) 
at java.awt.EventQueue$4.run(EventQueue.java:717) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) 
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) 

行217:

 public ImageIcon(URL location){ 
    this(location, location.toExternalForm());} 

行527:

map.put(s, new ImageIcon(getClass().getResource("/images/right.png"))); 

我檢查的System.out.println(的getClass()的getResource( 「/圖片/ right.png」)。);並返回null。 此外,我複製此圖片在bin文件夾,但錯誤是相同的。爲什麼?請幫幫我!

+0

爲使getClass()。getResource()正常工作,圖標需要與.class文件位於同一目錄樹中。你如何編寫你的應用程序? – yole 2015-02-10 08:08:03

+0

你看過文檔或樣例用法嗎? – keyser 2015-02-10 08:08:37

+0

不要在任何地方手動放置任何.png文件。使用Ant,Maven,Gradle或者您最喜歡的IDE來爲您編譯項目。另外我猜想你的第二個代碼片段中的路徑在開始時不應該包含「resources」。 – yole 2015-02-10 08:25:11

回答

1

getResource()查找具有給定名稱的資源。用於搜索與給定類關聯的資源的規則由類的定義類加載器來實現。

確保您的文件存在於類路徑或項目中。

相關問題