0

I'm creating a method that is supposed to find the path to a text file and return it as a String to be used for a configuration file. Here's the block of code that is being affected:

public String getConfigLocation() {
        String fileName = "locateconfig.txt";
        ClassLoader classLoader = ClassLoader.getSystemClassLoader();
        File file = new File(classLoader.getResource(fileName).getFile());
        locationFile = file.getAbsolutePath();
        return locationFile;
    }

The line that's causing this exception to be thrown is File file = new File(classLoader.getResource(fileName).getFile());.

Here's the first few lines of the stack trace - just the first few, because all the methods being affected here interact with the one I'm having a problem with.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at epicorexpert.EpicoreJFrame.getConfigLocation(EpicoreJFrame.java:1068)
    at epicorexpert.EpicoreJFrame.checkConfig(EpicoreJFrame.java:1076)
    at epicorexpert.EpicoreJFrame.<init>(EpicoreJFrame.java:94)

What probably-obvious issue am I overlooking? Thanks.

Edit: Why is this being marked as a duplicate question and suggesting a redirect for the thread on what NullPointerExceptions are? I understand what they are and how they're typically resolved. That's not what I'm asking here; this is specifically about the ClassLoader and File objects and why I'm getting NPEs from them.

ADO
  • 35
  • 8
  • Isn't the `classLoader` null? – Sk1X1 Sep 21 '19 at 21:00
  • Oh, word, that's probably it. Told you it was a probably-obvious issue. – ADO Sep 21 '19 at 21:03
  • Yes, that is likely to be your case. –  Sep 21 '19 at 21:03
  • Changed the declaration to `ClassLoader classLoader = this.getClass().getClassLoader();`, but it's still not working out as it should. I'm pretty sure using getSystemClassLoader() shouldn't be returning null either :( – ADO Sep 21 '19 at 21:06
  • Can ```classLoader.getResource(fileName)``` return null? –  Sep 21 '19 at 21:11
  • Why not put a println to check if classLoader is null, then on the getResources part then do a file exists on the getFile(). We can't see your project but it obviously has to be in one of these three areas. – James Black Sep 21 '19 at 21:16
  • James, I did that and it looks like the classLoader isn't null - it's printing its value as 'sun.misc.Launcher$AppClassLoader@750159', which sounds, uh, about right. One answer suggested it has something to do with classLoader being unable to find the file as opposed to there being an issue with how it was instantiated. Enrico's probably right about getResource returning null because the file I'm looking for isn't being found. – ADO Sep 21 '19 at 21:20
  • You don't need to return a global variable - String locationFile! That's useless and could cause problems as well. –  Sep 21 '19 at 21:21
  • Fixed that up right quick for elegance's sake :P – ADO Sep 21 '19 at 21:22

1 Answers1

0

getResource returns null if the file is not found! That's most probably the cause of your NullPointerException!

csabinho
  • 1,451
  • 1
  • 13
  • 22
  • I didn't even consider this. Let me move the file I'm trying to find to a folder that isn't nested in the deepest bowels of my PC and try again, or, like, create a new file on my desktop and see if that works. – ADO Sep 21 '19 at 21:13
  • Doesn't seem like that's it either :( – ADO Sep 21 '19 at 21:16
  • Could you "unnest" the `classLoader.getResource(fileName).getFile()`-call and output it? But this shouldn't be `null` as `getFile()` returns an empty string if it's not found. – csabinho Sep 21 '19 at 21:24
  • Tried that out too. Looks like it's just the case that the file I'm looking for can't be located on my PC, for whatever reason - figured that out by switching to using getPath() on the File object and receiving a FIleNotFoundException. Regardless of my continued struggles, you DID give the answer that helped me figure out what's wrong 0:-) – ADO Sep 21 '19 at 21:34