1 year ago

#371724

test-img

gluhtuten

Why do I get NullPointerException when trying to access resource file?

I want to make so a file in the program is packaged in the jar file because it is needed in my program. It is the firebase credentials file. I read that I need to add it to the resource folder but now I cannot access it because I get NullPointerException. I think the path is valid and everything seems okay but I get null every time. Here is the code:

ClassLoader classLoader = EmailSender.class.getClassLoader();
File file = new File(Objects.requireNonNull(classLoader.getResource("/parkingsystem-cf164-firebase-adminsdk-5jjuk-2be72bfcce.json")).getFile());

And here is the code structure: code structure

If there is another method to add file to jar I am open to hear it, it is just what I found on the Internet but it does not work for me. Any help is appreciated!

Edit: So apparently it works when the / is removed but then I need to convert it to FileInputStream and there I get FileNotFoundExcpetion. The rest of the code:

    FileInputStream serviceAccount = null;
    try {
        serviceAccount = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }


    FirebaseOptions options = null;
    try {
        options = new FirebaseOptions.Builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                .build();
    } catch (IOException e) {
        e.printStackTrace();
    }

java

jar

resources

0 Answers

Your Answer

Accepted video resources