由于 NullPointerException 而无法加载文件
Can't load files because of NullPointerException
晚上好,我目前正在处理我的作业,我花了好几个小时才发现这个错误。我试着抓住 NPE 也没有用。我错过了什么?
该方法的目的是加载文件夹 "templates" 中所有后缀为“.txt”的文件作为 class CuttingTemplate 的对象。然后将读取模板中的名称设置为读取文件的方向,因此当文件 template123.txt 在文件夹 "templates" 中时,CuttingTemplate 的名称设置为 "templates/template123.txt";
然后所有加载的 CuttingTemplates 都保存在这个列表中:
protected LinkedList<CuttingTemplate> templates = new LinkedList<CuttingTemplate>();
捕获异常(除了我无法弄清楚的 NPE)。
方法如下:
这是堆栈跟踪:
enter image description here
希望你能帮帮我。
你在OS什么? Unix 还是 Windows?有些东西告诉我那个文件夹可能不存在,因为你提供的那个路径不是我对这两个环境的期望。
在 Windows 中,您的路径可能如下所示:
C:/Users/username/Downloads/projectname/templates
在 Unix 中它应该是这样的:
/home/username/Downloads/projectname/templates
NPE 在函数的第 5 行抛出,因为第 4 行的 listFiles 函数返回 null,这是因为,您正在读取类路径中的文件夹,该文件夹不可用,而不是您拥有的路径第 1 行中提到。
晚上好,我目前正在处理我的作业,我花了好几个小时才发现这个错误。我试着抓住 NPE 也没有用。我错过了什么?
该方法的目的是加载文件夹 "templates" 中所有后缀为“.txt”的文件作为 class CuttingTemplate 的对象。然后将读取模板中的名称设置为读取文件的方向,因此当文件 template123.txt 在文件夹 "templates" 中时,CuttingTemplate 的名称设置为 "templates/template123.txt"; 然后所有加载的 CuttingTemplates 都保存在这个列表中:
protected LinkedList<CuttingTemplate> templates = new LinkedList<CuttingTemplate>();
捕获异常(除了我无法弄清楚的 NPE)。
方法如下:
这是堆栈跟踪:
enter image description here
希望你能帮帮我。
你在OS什么? Unix 还是 Windows?有些东西告诉我那个文件夹可能不存在,因为你提供的那个路径不是我对这两个环境的期望。
在 Windows 中,您的路径可能如下所示:
C:/Users/username/Downloads/projectname/templates
在 Unix 中它应该是这样的:
/home/username/Downloads/projectname/templates
NPE 在函数的第 5 行抛出,因为第 4 行的 listFiles 函数返回 null,这是因为,您正在读取类路径中的文件夹,该文件夹不可用,而不是您拥有的路径第 1 行中提到。