rest template exchange method null pointer exception

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


rest template exchange method null pointer exception



I am trying the hit the url with rest template exchange but getting null pointer exception at point of executing resttemplate.exchange method.Can you please let me know why i am getting the below null pointer exception



I am trying the hit the url with rest template exchange but getting null pointer exception at point of executing resttemplate.exchange method.Can you please let me know why i am getting the below null pointer exception



I am trying the hit the url with rest template exchange but getting null pointer exception at point of executing resttemplate.exchange method.Can you please let me know why i am getting the below null pointer exception



I am trying the hit the url with rest template exchange but getting null pointer exception at point of executing resttemplate.exchange method.Can you please let me know why i am getting the below null pointer exception



I am trying the hit the url with rest template exchange but getting null pointer exception at point of executing resttemplate.exchange method.Can you please let me know why i am getting the below null pointer exception
I am trying the hit the url with rest template exchange but getting null pointer exception at point of executing resttemplate.exchange method.Can you please let me know why i am getting the below null pointer exception
I am trying the hit the url with rest template exchange but getting null pointer exception at point of executing resttemplate.exchange method.Can you please let me know why i am getting the below null pointer exception



Exception details :


Reached inside ===
Reached === 2
Reached === 3
java.lang.NullPointerException
at com.test.resttemplate.RestTempaltetestApplication.callRestTemplateExchange(RestTempaltetestApplication.java:54)
at com.test.resttemplate.RestTempaltetestApplication.main(RestTempaltetestApplication.java:29)



Here is my code :


package com.test.resttemplate;

import java.io.IOException;
import java.util.Map;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@SpringBootApplication
public class RestTempaltetestApplication {
private static RestTemplate restTemplate;
@SuppressWarnings("unchecked")
public static void main(String args) {
SpringApplication.run(RestTempaltetestApplication.class, args);

final HttpHeaders headers = new HttpHeaders();
headers.add("materialId", "122344");
Map<String, Object> map;
try {
map = (Map<String, Object>) callRestTemplateExchange("https://alloyui.com/io/data/states.json", headers, HttpMethod.GET, null, Map.class);
System.out.println("Keyssss :"+map.keySet()+"values"+map.values());

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

@SuppressWarnings("unchecked")
public static Object callRestTemplateExchange(String url, HttpHeaders headers, HttpMethod method, Object body,
Class<?> claszz) throws Exception {
try {
System.out.println("Reached inside === ");
ResponseEntity<Object> response = null;
HttpEntity<?> requestEntity = new HttpEntity<Object>(headers);
System.out.println("Reached === 2");
if (null != body) {
System.out.println("Request === " + convertObjectToString(body));
requestEntity = new HttpEntity<Object>(body, headers);
} else {
requestEntity = new HttpEntity<Object>(headers);
}
System.out.println("Reached === 3");
response = (ResponseEntity<Object>) restTemplate.exchange(url, method, requestEntity, claszz);
if(null!=response.getBody()){
System.out.println("response === " + response.getBody());
}else
System.out.println("response is null");
System.out.println("Reached === 4");
return response.getBody();
} catch (HttpClientErrorException e) {

throw new Exception();
}
}


public final static String convertObjectToString(Object object) {
if (null != object) {
ObjectMapper objectMapper = new ObjectMapper();
try {
Object json = objectMapper.readValue(objectMapper.writeValueAsString(object), Object.class);
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
} catch (JsonMappingException e) {

} catch (IOException e) {

}
}
return null;
}
}









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

Blogger: Contempo template have managed to get the posts to post in full but there is still a “snippet” of the post at the bottom of each full post