2014-12-03 144 views
0

我得到一个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; 
} 

回答

0

你能分享你的spring配置文件吗?

你有restter和setter for restClient吗?

您可以按照以下步骤进行调试: - 1. Autowire其他一些bean,只是为了检查ShowRestClient类是否是合适的spring bean。 2.检查您注入ShowRestClient的类是否是合适的bean。

如果可能,分享堆栈跟踪,以便我们了解正在发生的事情。

+0

“2.检查您注入的类ShowRestClient是合适的bean“那! :) – 2014-12-03 11:22:00

+0

这会发生一些时候,人们错误地使用new运算符创建类的对象,并尝试在该类中自动装入或注入依赖。 – Panther 2014-12-04 18:39:56

1

你RESTClient实现似乎null最有可能的,因为@Autowired没有工作。我怀疑你的组件不是一个真正的Spring Bean,因为如果它是,那么在应用程序启动过程中你的上下文创建将失败。确保您已将软件包包含在软件包扫描中。

+0

对不起,我完全是一个新手在休息,你是什么意思与软件包包括在包扫描? 你能告诉我一些我可以尝试的步骤吗? – 2014-12-03 00:47:09

+1

@Akin:你遇到了Spring配置问题,而不是REST问题。阅读Spring自动装配和类路径扫描。 – 2014-12-03 00:48:53

+0

我检查,但完全是它应该是... – 2014-12-03 02:12:34

相关问题