Search code examples
javaspringamazon-s3nullpointerexceptionreturn

Cannot return because of null value. java.lang.NullPointerException: Cannot invoke "reactor.core.publisher.Mono.map" because is null


When i try to login, the i do a version check, if the version is not the same i generate and return a download url using the s3 presigner method. but it throws an error saying, it cannot return the url as jwtoken is null. How do i map the url into jwtoken object so that i can return it. Error:

java.lang.NullPointerException: Cannot invoke "reactor.core.publisher.Mono.map(java.util.function.Function)" because "jwtoken" is null

Login Method:

 public Mono<JwToken> login(String currentVersion) throws Exception {
 Mono<JwToken> jwtoken = null;
if(!currentVersion.equals(storageController.getLatestClientVersion()))
 {
 log.info("Client doesn't have the latest version of mobile client. Sending the download URL");
URL url = storageService.generatePreSignedUrl();
log.info("URL received is:{}",url);
 return  jwtoken.map(response -> JwToken.builder().downloadUrl(url).build());
 } 
else
 {
   jwtoken = initiateAuth(auth, AuthFlowType.USER_PASSWORD_AUTH).map(response -> JwToken.builder().accessToken(response.idToken()).refreshToken(response.refreshToken())
                                    .tokenType(response.tokenType()).expiresIn(response.expiresIn()).reponseMessage("JwToken successfully returned").build());;
   return jwtoken;
 }
}

JwToken model class:

public class JwToken {


    private final String accessToken;
    
    private final String refreshToken;

    private final Integer expiresIn;

    private final String tokenType;

    private final String reponseMessage;

    private final URL downloadUrl;
}

generatePreSignedUrl() method:

public URL generatePreSignedUrl() throws ExecutionException, InterruptedException {

        AwsBasicCredentials creds = AwsBasicCredentials.create(Config.getAwsAccessKey(),Config.getAwsSecretKey());
        S3Presigner presigner = S3Presigner.builder().region(Region.us).credentialsProvider(StaticCredentialsProvider.create(creds)).build();
        String fileKey =getApkFileKey();
        GetObjectRequest getObjectRequest =
                GetObjectRequest.builder()
                        .bucket(Config.getBucketName())
                        .key(fileKey)
                        .build();

        // Create a GetObjectPresignRequest to specify the signature duration
        GetObjectPresignRequest getObjectPresignRequest =
                GetObjectPresignRequest.builder()
                        .signatureDuration(Duration.ofMinutes(5))
                        .getObjectRequest(getObjectRequest)
                        .build();

        // Generate the presigned request
        PresignedGetObjectRequest presignedGetObjectRequest =
                presigner.presignGetObject(getObjectPresignRequest);


        URL downloadUrl = presignedGetObjectRequest.url();
        log.info("Download url is: {}",downloadUrl);
        presigner.close();
        return downloadUrl;
    }```

Solution

  • Seems logic ^^ In the 1st condition jwtoken is not initialized

    Mono<JwToken> jwtoken = null
    //...
    return jwtoken.map...