Java 无法创建相对路径
项目的“我的生成”文件夹具有以下结构:Java 无法创建相对路径,java,swing,relative-path,Java,Swing,Relative Path,项目的“我的生成”文件夹具有以下结构: build |-classes |package |-HelloWorld.class |-resources |-images |-image.png HelloWorld类中有ImageIcon对象,其声明应如下所示: ImageIcon icon = new ImageIcon(HelloWorld.class.getResource("../../res
build
|-classes
|package
|-HelloWorld.class
|-resources
|-images
|-image.png
HelloWorld类中有ImageIcon对象,其声明应如下所示:
ImageIcon icon = new ImageIcon(HelloWorld.class.getResource("../../resources/images/picture.png"));
但它在getResource方法中抛出NullPointerException。我哪里出错了?没有比@nIcE cOw+++Image更好的东西了,因为它位于类路径中,所以您不应该要求相对路径new-ImageIconHelloWorld.class.getResource/resources/images/picture.png;