1

First of all the code snippets below are part of a Google Cloud Project application and are running on my local client which is a Raspberry Pi 1. In order to be able to send data from a sensor connected to the Pi to the cloud an authorization is needed. All needed client secrets are stored in the "client_secrets.json" which is in src/main/resources.

Project Hierarchy

enter image description here

While trying to use the clients secrets to authorize the below code throws a NullPointerException. It is part of the class "CmdLineAuthenticationProvider" (see Project Hierarchy).

InputStreamReader reader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream(this.clientSecretsFile));

This is probably just a path related error but none of my attempts solving it worked (I tried to adjust the path and also copied the client_secrets.json to different locations in the hope that it finds it). The "clientSecretsFile" gets set to "/client_secret.json" in the "RaspiApp" class.

CmdLineAuthenticationProvider provider = new CmdLineAuthenticationProvider();

    provider.setClientSecretsFile("client_secret.json");
    provider.setScopes(SCOPES);

    // get the oauth credentials using the client secrets
    Credential credential = provider.authorize();

In my pom.xml I specified the resources as follows:

<sourceDirectory>src/main/java</sourceDirectory>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <targetPath>${project.build.directory}/classes</targetPath>
            <filtering>false</filtering>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
    </resources>

Complete error code:

java.lang.NullPointerException
    at java.io.Reader.<init>(Reader.java:78)
    at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
    at de.econfood.pi.app.CmdLineAuthenticationProvider.getCredential(CmdLineAuthenticationProvider.java:102)
    at de.econfood.pi.app.CmdLineAuthenticationProvider.authorize(CmdLineAuthenticationProvider.java:64)
    at de.econfood.pi.app.RaspiApp.getSensorEndpoint(RaspiApp.java:171)
    at de.econfood.pi.app.RaspiApp.sendSensorData(RaspiApp.java:144)
    at de.econfood.pi.app.RaspiApp.onGetRecsets(RaspiApp.java:126)
    at de.econfood.pi.app.BrmReadThread.readBuffer(BrmReadThread.java:112)
    at de.econfood.pi.app.BrmReadThread.run(BrmReadThread.java:20)
    at java.lang.Thread.run(Thread.java:745)
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Zotac386
  • 25
  • 2
  • 10
  • 1
    ClassLoader.getResourceAsStream() doesn't expect a file path. It doesn't return a stream over a file. It returns a stream over a resource, that must be available in the classpath. And the path must not start with a `/`. – JB Nizet Mar 02 '18 at 11:52
  • check where the file is after compilation. it shoul be under target/classes/secret.json – Murat Can ALPAY Mar 02 '18 at 11:58
  • @JBNizet You're absolutely right! The leading / was more like a desperate attempt to solve this issue, but I receive the exact same error without it. I'll edit the question in order to avoid similar suggestions. – Zotac386 Mar 02 '18 at 12:02
  • Why do you configure the resource target to be something other than the default? – JB Nizet Mar 02 '18 at 12:14
  • I used this project for guidance: [link to github](https://github.com/omerio/raspberrypi-app). It's almost the same use case except the sensors used and he defined the resources like that. I also tried to comment out the without success. – Zotac386 Mar 02 '18 at 12:41
  • I still can't solve this... Also tried without the ClassLoader, like in the [Google example](https://developers.google.com/api-client-library/java/google-api-java-client/samples) @MuratCanALPAY when looking into my workspace folder it's placed right there but the structure in the JAR after exporting is a different one. Maybe it's got something to do with the export to jar? – Zotac386 Mar 02 '18 at 14:29
  • try giving the path like it's in the jar and running from the jar. It looks like your maven settings move the file somewhere else. – Murat Can ALPAY Mar 05 '18 at 09:24

2 Answers2

0

A path starting with "/" is normally seen as an absolute path. What you need is a relative path, so omit the leading "/".

kutschkem
  • 7,826
  • 3
  • 21
  • 56
0

I was able to solve this by myself after two days of troubleshooting...

The cause of the error was a faulty configuration of the POM.xml which caused that the client_secret.json was not located where it should have been in the JAR and consequently it was impossible to find it there.

Zotac386
  • 25
  • 2
  • 10