Search code examples
javaexceptionoracle-cloud-infrastructureobject-storage

Oracle Object Storage Client NullPointerException on request


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)

Solution

  • The endpoint requires the region to be defined. 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

    The config file that was used for Python SDK would have 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