0

Good evening, I'm currently working on my assignment and I have been looking for hours finding this error. I tried catching the NPE which didn't work either. What am I missing?

The method's purpose is to load all files in the folder "templates" which have the suffixes ".txt" as objects from the class CuttingTemplate. The name's from the read templates are then set to the direction of the read file, so when the file template123.txt is in the folder "templates" the CuttingTemplate's name is set to "templates/template123.txt"; All loaded CuttingTemplates then are saved in this list:

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

Exceptions are caught (except the NPE which I couldn't figure out).

So here is the method:

Here is the stack trace:

enter image description here

I hope you can help me.

  • 1
    You are catching and swallowing an NPE without printing the stacktrace, making it hard to debug. Please see this [post](https://stackoverflow.com/questions/18265940/when-is-it-ok-to-catch-nullpointerexception) on whether you should ever catch an NPE. Please provide the stacktrace once you have it, though this is likely to be closed as a duplicate of this [post](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it). – Nexevis Jan 24 '20 at 15:37
  • Please post the exception stacktrace – Willem Jan 24 '20 at 15:38
  • Add the stack trace to your question by copying and pasting the text itself, not as an image. – VGR Jan 24 '20 at 16:01
  • I'm pretty new to Stack trace in Java, my Stack Trace Console in Eclipse is blank for some reason. – silentstorm Jan 24 '20 at 16:32

2 Answers2

0

What OS are you on? Unix or Windows? Something tells me that that folder probably doesn't exist, since that path you provided is not what I'd expect from those two environments.

In Windows, your path should probably look like this:

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

And in Unix it should look like this:

    /home/username/Downloads/projectname/templates
  • Okay so I'm on Windows and I adjusted the file path to the backslash notation but with double backslashes, as I'm getting a compile time error but I still get the NPE... – silentstorm Jan 24 '20 at 16:32
  • try with **System.out.println(templatesFolder.exists());** after you declare the variable **templatesFolder**: if it prints "false", then the folder's path is still wrong. If, on the other hand, it prints "true", then the problem is elsewhere – Fabrizio Roman Jan 24 '20 at 16:40
  • Thank you for this, I know now that the path is apparently false – silentstorm Jan 24 '20 at 16:45
  • Thank you, I didn't had the idea of printing that! Now I am aware of my stupidness: the directory I lead it into was a zip file, so I unpacked this and changed the string. I am so relieved, I feel like I beat Dark Souls. – silentstorm Jan 24 '20 at 17:16
  • Did you solve your problem with my answer? If yes, mark it as the solution, otherwise feel free to add any info that can help us help you – Fabrizio Roman Jan 25 '20 at 06:16
-1

NPE is thrown at line#5 of the function, since line#4 is having null returned by listFiles function and it's coz, you are reading the folder in classpath, which is not available, instead of the path you have mentioned in line 1.