我得到一個NullpointerException但我甚至不明白它怎麼可能。我只是從其他類複製粘貼部分RestTemplate restTemplate = this.restClient.getRestTemplate(),但在這裏我得到一個NullPointerException?!RestClient拋出NullPointerException?
@Component
public class ShowRestClient implements ShowService{
@Autowired
private RestClient restClient;
@Override
public List<ShowDto> getShowsByPerformanceID(int perfID)
throws ServiceException {
// TODO Auto-generated method stub
return null;
}
@Override
public List<ShowDto> findAllShows() throws ServiceException {
RestTemplate restTemplate = this.restClient.getRestTemplate();(!!!!! HERE)
String url = this.restClient.createServiceUrl("/show/");
HttpEntity<String> entity = new HttpEntity<String>(this.restClient.getHttpHeaders());
List<ShowDto> shows = null;
try {
ParameterizedTypeReference<List<ShowDto>> ref = new ParameterizedTypeReference<List<ShowDto>>() {};
ResponseEntity<List<ShowDto>> response = restTemplate.exchange(URI.create(url), HttpMethod.GET, entity, ref);
shows = response.getBody();
} catch (RestClientException e) {
throw new ServiceException("Could not retrieve shows: " + e.getMessage(), e);
}
return shows;
}
「2.檢查您注入的類ShowRestClient是合適的bean「那! :) – 2014-12-03 11:22:00
這會發生一些時候,人們錯誤地使用new運算符創建類的對象,並嘗試在該類中自動裝入或注入依賴。 – Panther 2014-12-04 18:39:56