Currently I'm working with SonarQube solving issues but I'm facing a trouble to how handle null pointers that shouldn't be dereferenced. This issues is displayed by SonarQube.
My main problem is because I'm doing a restTemplate.exchange with try-catch and declaring a variable with null value before the clause try and then using it inside the try. Finally my method is returning a response with value of restTemplate.
public MyDto exchangeUrlRequest(String url){
ResponseEntity<MyDto> responseDto = null;
try{
responseDto = restTemplate.exchange(url, HttpMethod.PUT...
}catch(HttpClientErrorException e){
//some code here
}
return responseDto.getBody();
}
The expected result here is solve the issue with sonarqube. How handle the initialization of "responseDto" without "null" because is throwing the issue on sonar.
I already tried to put ResponseEntity<MyDto> responseDto inside my try clause assigning and returning the respective value but its mandatory to return something out the try/catch. Put a new ResponseEntity is wrong because I won't know what will be the answer for the status of http.