我试图访问我的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