oracle / oci-java-sdk

Oracle Cloud Infrastructure SDK for Java
https://cloud.oracle.com/cloud-infrastructure
Other
201 stars 156 forks source link

Object Storage Client NullPointerException on Request #527

Closed heinGertenbach closed 1 year ago

heinGertenbach commented 1 year ago

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

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)
VidhiBhansali commented 1 year ago

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:

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

heinGertenbach commented 1 year ago

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

VidhiBhansali commented 1 year ago

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