0

My project structure is:

Project
  src
    main
      java
        youtube
          ApiExample.java  
      resources
        client_secret.json

I am trying to use the code sample for getting videos from a playlist using the youtube data api. I keep getting a nullPointerException indicating Inputstream can't find the file but I am not sure how to access it. I am also using maven for the dependencies.

private static final String CLIENT_SECRETS= "client_secret.json";

InputStream in = ApiExample.class.getResourceAsStream(CLIENT_SECRETS);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
bluelog27
  • 15
  • 4
  • Is `resources` recognized as your resource location? – QBrute Jul 24 '21 at 11:46
  • Yes, resources is marked as my resources root. – bluelog27 Jul 24 '21 at 11:47
  • 1
    **tl;dr** of the dupe: you provide a relative name (i.e. one not starting with `/`), therefore it will try to load `youtube/client_secret.json` (using the package name of ÀpiExample` as a prefix (after converting it to a directory name). – Joachim Sauer Jul 24 '21 at 11:47
  • I've tried moving the client_secret.json file into my youtube package and also using absolute path with slashes but it still returns null. – bluelog27 Jul 24 '21 at 11:53
  • If it’s in the same package as ApiExample, don’t start the resource path with a slash. If it’s in the root package, you do need to start the resource path with a slash. (All of this behavior is explained in [the documentation](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/Class.html#getResourceAsStream(java.lang.String)).) – VGR Jul 24 '21 at 13:56
  • I got it to work by just having `client_secret.json` in resources folder as before but just adding a `/` before "client_secret.json" in the CLIENT_SECRETS String declaration. – bluelog27 Jul 24 '21 at 14:42

0 Answers0