javaintellij-ideanullpointerexceptionresources

IntelliJ - can't find resource, nullPointerException using getResource()


Essentially, I'm using (or trying to use) Freemarker, which requires me to set up a folder for templates using

cfg.setDirectoryForTemplateLoading(File dir);

I've created a folder /htmltemplates in src/main/resources/htmltemplates. The class I'm trying to access the resource from is in src/main/java.

However, when I try to use

URL url = getClass().getResource("/htmltemplates");
File dir = new File(url.toString());

I get a nullPointerException. I've tried various combinations of ClassLoader, tried using a file instead of a directory (useless, he finds nothing), searched on StackOverflow for over an hour... I had an almost identical setup on a previous occasion, everything worked swimmingly, but now - nothing. Attempts to work out what might be different have led to absolutely zilch.

I've been running the program through IntelliJ's build environment, also tried building a .jar artifact (which I'd been doing before, no problem) and then running it via console, nothing works. Any ideas why this is happening?

EDIT: This just keeps getting stranger. I've had other people at work try it out, we've both started new IntelliJ projects simultaneously, set them up identically, and they can't replicate my problem. What the hell?


Solution

  • I finally solved the issue by switching to Maven instead of using the built-in IntelliJ artifact builder. It works, but don't ask me why, I haven't the slightest.