Skip to content
Advertisement

error: no suitable method found for newArrayList(String)

I am fairly new to Java and trying to use a Google Cloud service. When I am trying to use an explicit way to point my service account following this guide from this; https://cloud.google.com/docs/authentication/production#passing_code

import com.google.api.client.util.Lists;
import com.google.api.gax.paging.Page;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.io.FileInputStream;
import java.io.IOException;



static void authExplicit(String jsonPath) throws IOException {
    // You can specify a credential file by providing a path to GoogleCredentials.
    // Otherwise credentials are read from the GOOGLE_APPLICATION_CREDENTIALS environment variable.
    GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
            .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
    Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

    System.out.println("Buckets:");
    Page<Bucket> buckets = storage.list();
    for (Bucket bucket : buckets.iterateAll()) {
        System.out.println(bucket.toString());
    }
}

I already have installed requirements. However, this code throws an error as follows;

error: no suitable method found for newArrayList(String)
    GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath)).createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
                                                                                                                  ^
method Lists.<E#1>newArrayList() is not applicable
  (cannot infer type-variable(s) E#1
    (actual and formal argument lists differ in length))
method Lists.<E#2>newArrayList(Iterable<? extends E#2>) is not applicable
  (cannot infer type-variable(s) E#2
    (argument mismatch; String cannot be converted to Iterable<? extends E#2>))
method Lists.<E#3>newArrayList(Iterator<? extends E#3>) is not applicable
  (cannot infer type-variable(s) E#3

    (argument mismatch; String cannot be converted to Iterator<? extends E#3>)) where E#1,E#2,E#3 are type-variables:

E#1 extends Object declared in method <E#1>newArrayList()
E#2 extends Object declared in method <E#2>newArrayList(Iterable<? extends E#2>)
E#3 extends Object declared in method <E#3>newArrayList(Iterator<? extends E#3>)

I am not sure what is wrong in my setting and no luck on searching an answer.

Advertisement

Answer

So a quick look at google’s documentation https://cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.Lists, it doesn’t contain a method newArrayList that takes a String as a parameter, but google has another library (guava) that contains the method you are looking for, https://guava.dev/releases/19.0/api/docs/com/google/common/collect/Lists.html, so replace your import com.google.api.client.util.Lists with import com.google.common.collect.Lists;;

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement