0

I have a problem using the Google Cloud Storage in my app, basically where I'm failing is retrieving the GoogleCredential from this line, that is giving me a NullPointerException =

GoogleCredential credential = new GoogleCredential.Builder()

I'm trying to get the Google Application Default Credentials, but I don´t have to a Google Compute Engine, I would prefer using Google App Engine.

I'm using that line of code for making this method that is the responsable for the uploading and downloading the file =

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;
    }

to add more into the question, after debugging I noticed that the line that is giving me the nullPointerException is in this method =

private static Properties getProperties() throws Exception {

        if (properties == null) {
            properties = new Properties();
            InputStream stream = CloudStorage.class
                    .getResourceAsStream("/cloudstorage.properties");
            try {
                properties.load(stream);
            } catch (IOException e) {
                throw new RuntimeException(
                        "cloudstorage.properties must be present in classpath",
                        e);
            } finally {
                stream.close();
            }
        }
        return properties;
    }

specifically this line of code =

InputStream stream = CloudStorage.class
                        .getResourceAsStream("/cloudstorage.properties");
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Johanna
  • 5
  • 1
  • 5
  • to add more into the question, after debugging I noticed that the line that is giving me the nullPointerException is in this method = – Johanna Mar 26 '16 at 17:52
  • `stream.close()` looks like it will throw a NullPointerException. – OneCricketeer Mar 28 '16 at 21:36
  • yes!!! thats exactly what happens!!! please help – Johanna Mar 30 '16 at 00:48
  • Just surround that line if a check for `if (stream == null)`. You initialize the stream within the try block, so if an exception happens, then the stream will be null in the finally block. Your actual exception is elsewhere, though . – OneCricketeer Mar 30 '16 at 01:10
  • I just did that, but what is the proper code that I have to write that creates the stream? – Johanna Mar 30 '16 at 03:05
  • Not sure, but you may want to try removing the slash in the front of `"/cloudstorage.properties"` – OneCricketeer Mar 30 '16 at 03:20
  • does not work, the full path is MyApp\app\src\main\res\cloudstorage.properties – Johanna Mar 30 '16 at 03:35
  • I must've skipped over the fact this is Android. You should be using an Asset directory instead of the res folder and use the AssetManager to read that file instead of getResourceAsStream. You can see here for more information. http://stackoverflow.com/a/17817063/2308683 – OneCricketeer Mar 30 '16 at 03:42

0 Answers0