oracle/oci-java-sdk

Object Storage Client NullPointerException on Request

heinGertenbach opened this issue · 3 comments

I am setting up my object storage client as described by the docs:

  • I'm using sdk version 3.17.0
  • Tested in java 17 & 19
  • I've used the config file with the python sdk and it was succesfull
ConfigFile ociConfig = ConfigFileReader.parse(configFile.getAbsolutePath(), profile);

Supplier<InputStream> privateKeySupplier = new SimplePrivateKeySupplier(ociConfig.get("key_file"));
AuthenticationDetailsProvider provider = SimpleAuthenticationDetailsProvider.builder()
        .tenantId(ociConfig.get("tenancy"))
        .userId(ociConfig.get("user"))
        .fingerprint(ociConfig.get("fingerprint"))
        .privateKeySupplier(privateKeySupplier)
        .build();

objectStorageClient = ObjectStorageClient
        .builder()
        .build(provider);

Runtime.getRuntime().addShutdownHook(new Thread(() -> {
    if (objectStorageClient != null)
        objectStorageClient.close();
}));

namespace = handleResponse(objectStorageClient.getNamespace(GetNamespaceRequest.builder().build())).getValue();

When I try running the above code sample I get the following stack trace.

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.contains(java.lang.CharSequence)" because "endpoint" is null
        at com.oracle.bmc.http.internal.BaseClient.populateServiceParametersInEndpoint(BaseClient.java:215)
        at com.oracle.bmc.objectstorage.ObjectStorageClient.getNamespace(ObjectStorageClient.java:874)

Hi @heinGertenbach - The endpoint requires the region to be defined. Can you please provide the region and try again? You can set the region in one of the following two ways:

  • when you are defining the provider
AuthenticationDetailsProvider provider = SimpleAuthenticationDetailsProvider.builder()
        .tenantId(ociConfig.get("tenancy"))
        .userId(ociConfig.get("user"))
        .fingerprint(ociConfig.get("fingerprint"))
        .privateKeySupplier(privateKeySupplier)
        .region(Region.US_PHOENIX_1)     // please add your region here
        .build();
  • when you are setting up the client
objectStorageClient = ObjectStorageClient
        .builder()
        .region(Region.US_PHOENIX_1)      // please add your region here
        .build(provider);

I've used the config file with the python sdk and it was successful

I believe that the config file that was used for Python SDK had information about the region. You can try to use the same config file for OCI Java SDK by using ConfigFileAuthenticationDetailsProvider instead of SimpleAuthenticationDetailsProvider. For example on how to use ConfigFileAuthenticationDetailsProvider for Object Storage client, please refer ObjectStorageGetNamespaceExample.java

Thank you for the response. Indeed it worked after that, I didn't add it since it wasn't in the documentation?

Glad it worked! The Requirements section in SDK for Java docs has details on how to configure the SDK. You can also directly take a look at the basic entries that are required for the configuration file here which states that region is a required information
Another documentation that can help regarding the endpoints is Setting the Endpoints