我試圖訪問我的Java代碼中的一些圖像。下面是調用圖像的代碼。訪問Eclipse中的圖像路徑
public final ImageIcon whitePiece = new ImageIcon(getClass().getResource("Images/whitecircle.PNG"));
public final ImageIcon blackPiece = new ImageIcon(getClass().getResource("Images/blackcircle.png"));
public final ImageIcon tiePiece = new ImageIcon(getClass().getResource("Images/tiecircle.PNG"));
public final ImageIcon boardPic = new ImageIcon(getClass().getResource("Images/gameboard.PNG"));
然而,這將返回以下錯誤:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at jared.othello.GameInterface.<init>(GameInterface.java:26)
at jared.othello.Main.main(Main.java:22)
線26上面的代碼中的第一行。我幾乎肯定我的文件目錄命名有問題,但我無法弄清楚有什麼問題或如何解決它。我已經建立了我的項目中名爲「圖像」源文件並放在影像的文件夾中,像這樣:
我在做什麼錯,什麼是正確的文件名語法?謝謝!
你需要在圖像路徑中領先的'/'? – 2013-04-08 21:52:36
不,我把它拿出來了,但代碼仍然不起作用。 – 2013-04-08 21:55:06
您需要創建另一個源路徑,並將圖像文件夾放在其中。然後確保Eclipse將這些資源包含在已編譯的工件中,因爲有時候Eclipse會添加一些過濾器來阻止某些文件出現在您的構建中。 – 2013-04-08 21:56:21