Android 正在尝试读取cloudstorage.properties。获取NullPointerException
我在应用程序中使用谷歌云存储时遇到问题,基本上我失败的地方是从这行检索谷歌凭证,这给了我一个NullPointerException=Android 正在尝试读取cloudstorage.properties。获取NullPointerException,android,google-cloud-storage,Android,Google Cloud Storage,我在应用程序中使用谷歌云存储时遇到问题,基本上我失败的地方是从这行检索谷歌凭证,这给了我一个NullPointerException= GoogleCredential credential = new GoogleCredential.Builder() 我正在尝试获取谷歌应用程序的默认凭据,但我不必使用谷歌计算引擎,我更喜欢使用谷歌应用程序引擎 我使用这行代码来制作这个方法,它负责上传和下载文件= private static Storage getStorage() {
GoogleCredential credential = new GoogleCredential.Builder()
我正在尝试获取谷歌应用程序的默认凭据,但我不必使用谷歌计算引擎,我更喜欢使用谷歌应用程序引擎
我使用这行代码来制作这个方法,它负责上传和下载文件=
private static Storage getStorage() {
if (storage == null) {
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
List<String> scopes = new ArrayList<>();
scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);
try {
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(
getProperties().getProperty(ACCOUNT_ID_PROPERTY))
.setServiceAccountPrivateKeyFromP12File(
new File(getProperties().getProperty(
PRIVATE_KEY_PATH_PROPERTY)))
.setServiceAccountScopes(scopes).build();
storage = new Storage.Builder(httpTransport, jsonFactory,
credential).setApplicationName(
getProperties().getProperty(APPLICATION_NAME_PROPERTY))
.build();
} catch (Exception e) {
e.printStackTrace();
}
}
return storage;
}
特别是这一行代码=
InputStream stream = CloudStorage.class
.getResourceAsStream("/cloudstorage.properties");
为了进一步说明这个问题,在调试之后,我注意到提供nullPointerException的行位于以下方法中=
stream.close()
看起来它将抛出nullPointerException。是的!!!这就是发生的事情!!!如果检查if(stream==null)
,请将该行环绕。在try块中初始化流,因此如果发生异常,那么finally块中的流将为null。不过,您的实际例外情况在别处。我刚刚这么做了,但是我必须编写什么样的代码来创建流呢?
InputStream stream = CloudStorage.class
.getResourceAsStream("/cloudstorage.properties");