由于nullpointerexception,无法加载文件

xienkqul  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(317)

晚上好,我正在做我的作业,我找了几个小时才发现这个错误。我试着抓住了npe,但也没有成功。我错过了什么?
该方法的目的是从类cuttingtemplate中加载后缀为“.txt”的文件夹“templates”中的所有文件作为对象。然后将读取模板中的名称设置为读取文件的方向,因此当template123.txt文件位于“templates”文件夹中时,cuttingtemplate的名称设置为“templates/template123.txt”;所有加载的切割模板将保存在此列表中:

protected LinkedList<CuttingTemplate> templates = new LinkedList<CuttingTemplate>();

异常被捕获(除了我无法理解的npe)。
方法如下:
以下是堆栈跟踪:
在此处输入图像描述
我希望你能帮助我。

gpnt7bae

gpnt7bae1#

你在玩什么游戏?unix还是windows?有些东西告诉我,该文件夹可能不存在,因为您提供的路径不是我从这两个环境中期望的路径。
在windows中,您的路径可能如下所示:

C:/Users/username/Downloads/projectname/templates

在unix中应该是这样的:

/home/username/Downloads/projectname/templates
3j86kqsm

3j86kqsm2#

npe被抛出到函数的第5行,因为第4行由listfiles函数返回null,这是因为您正在读取classpath中的文件夹,而不是第1行中提到的路径,该文件夹不可用。

相关问题