我在Linux上創建了一個項目,該項目使用各種資源(.png和.xml),當我在Linux上運行代碼時已成功加載,但一旦導出該項目到Windows它會引發空指針異常,當我嘗試做同樣的事情。Eclipse - 從Linux導入Windows的項目不加載資源
導出/導入是通過存檔文件機制完成的,我也嘗試手動導入源文件,創建文件夾並將資源文件導入它們。文件夾結構看起來完好無損,資源文件夾在兩個平臺上都被標記爲源文件夾。
而不是硬編碼文件分隔符我已經使用了依賴於系統的文件分隔符,所以應該沒有問題。
Misc。信息:Linux Mint的32位/ Windows 7的64位,日食帶的WindowBuilder,爪哇1.7(兩個平臺)
資源加載的例子:
public static void loadGameImages(TreeMap<String, ImageIcon> imageMap)
{
String dir_path = GameMechanics.class.getResource(
File.separator + "gameImages").toString(); // this is where the whole thing breaks
URI uri = null;
try
{
uri = new URI(dir_path);
}
catch (Exception ex)
{
raiseError(ex);
}
File dir = new File(uri.getPath());
File[] images = null;
if (dir.isDirectory())
images = dir.listFiles();
for (File f : images)
{
if (f.isFile())
imageMap.put(f.getName(), new ImageIcon(f.getPath()));
}
}
堆棧跟蹤:
java.lang.NullPointerException
at hangman.GameMechanics.loadGameImages(GameMechanics.java:98)
at hangman.MainWindow.<init>(MainWindow.java:90)
at hangman.MainWindow$1.run(MainWindow.java:65)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
究竟你的gameImages目錄在哪裏? –
@JunedAhsan它位於「資源」文件夾內。一切都是通過Eclipse添加的,而不是直接通過文件系統。 – Venom