InputStreamReader 抛出 NullPointerException
InputStreamReader throws NullPointerException
首先,下面的所有代码片段是 Google Cloud Project 应用程序的一部分,并且 运行 在我的本地客户端上 Raspberry Pi 1. 为了能够要将数据从连接到 Pi 的传感器发送到云端,需要授权。所有需要的客户端机密都存储在 src/main/resources.
中的 "client_secrets.json" 中
项目层次结构
在尝试使用客户端机密授权时,以下代码抛出 NullPointerException。它是 class "CmdLineAuthenticationProvider" 的一部分(请参阅项目层次结构)。
InputStreamReader reader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream(this.clientSecretsFile));
这可能只是一个与路径相关的错误,但 none 我尝试解决它的方法有效(我尝试调整路径并将 client_secrets.json 复制到不同的位置,希望它能找到它)。 "clientSecretsFile" 在 "RaspiApp" class 中设置为“/client_secret.json”。
CmdLineAuthenticationProvider provider = new CmdLineAuthenticationProvider();
provider.setClientSecretsFile("client_secret.json");
provider.setScopes(SCOPES);
// get the oauth credentials using the client secrets
Credential credential = provider.authorize();
在我的pom.xml中我指定了如下资源:
<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>
完整的错误代码:
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)
以“/”开头的路径通常被视为绝对路径。您需要的是相对路径,因此省略前导“/”。
经过两天的排查,我自己解决了这个问题...
错误的原因是 POM.xml 的错误配置导致 client_secret.json 没有位于它应该在 JAR 中的位置,因此不可能在那里找到它.
首先,下面的所有代码片段是 Google Cloud Project 应用程序的一部分,并且 运行 在我的本地客户端上 Raspberry Pi 1. 为了能够要将数据从连接到 Pi 的传感器发送到云端,需要授权。所有需要的客户端机密都存储在 src/main/resources.
中的 "client_secrets.json" 中项目层次结构
在尝试使用客户端机密授权时,以下代码抛出 NullPointerException。它是 class "CmdLineAuthenticationProvider" 的一部分(请参阅项目层次结构)。
InputStreamReader reader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream(this.clientSecretsFile));
这可能只是一个与路径相关的错误,但 none 我尝试解决它的方法有效(我尝试调整路径并将 client_secrets.json 复制到不同的位置,希望它能找到它)。 "clientSecretsFile" 在 "RaspiApp" class 中设置为“/client_secret.json”。
CmdLineAuthenticationProvider provider = new CmdLineAuthenticationProvider();
provider.setClientSecretsFile("client_secret.json");
provider.setScopes(SCOPES);
// get the oauth credentials using the client secrets
Credential credential = provider.authorize();
在我的pom.xml中我指定了如下资源:
<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>
完整的错误代码:
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)
以“/”开头的路径通常被视为绝对路径。您需要的是相对路径,因此省略前导“/”。
经过两天的排查,我自己解决了这个问题...
错误的原因是 POM.xml 的错误配置导致 client_secret.json 没有位于它应该在 JAR 中的位置,因此不可能在那里找到它.