我正在为我的项目制作可运行jar文件。尝试在Eclipse中使用资源时出现NullPointerException
代码
public class StandingsCreationHelper
{
private static final String TEMPLATE_FILENAME = "Standings_Template.xls";
public static void createStandingsFile() throws Exception
{
StandingsCreationHelper sch = new StandingsCreationHelper();
// Get the file from the resources folder
File templateFile = new File("TemporaryPlaceHolderExcelFile.xls");
OutputStream outputStream = new FileOutputStream(templateFile);
IOUtils.copy(sch.getFile(TEMPLATE_FILENAME), outputStream);
outputStream.close();
}
}
public InputStream getFile(String fileName)
{
return this.getClass().getClassLoader().getResourceAsStream(fileName);
}
public static void main(String[] args) throws Exception
{
createStandingsFile();
}
项目的结构
问题
当我打包我的代码在运行的JAR,我的计划将执行没有任何问题。但是,如果我从我的IDE(Eclipse)调用主方法,我会收到以下错误消息,就好像找不到资源:
线程“main”中的异常java.lang.NullPointerException at org.apache .poi.util.IOUtils.copy(IOUtils.java:182) 在standings.StandingsCreationHelper.createStandingsFile(StandingsCreationHelper.java:153) 在standings.StandingsCreationHelper.main(StandingsCreationHelper.java:222)
感谢预先任何帮助!
'“/resources/Standings_Template.xls”;'??? –
谢谢你的快速回答。改为提到的字符串仍然返回空指针异常。 –
什么是null,输入或输出? –