0

When I try to return a generic object i got a exception :

@RequestMapping(value="/administration/get_stat_all") 
public @ResponseBody
List<StatAllBean<String>>  get_stat_all(..) {

List<StatAllBean<String>> all_stats = new ArrayList<StatAllBean<String>>();
....
return all_stats ;

Here is my object :

public class StatAllBean<T> {

    public Map<T, Long> totalMap ;
    public Date date;

The exception :

19:14:56.366 [http-nio-8080-exec-9] DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [<java.lang.reflect.MalformedParameterizedTypeException>]: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]); nested exception is org.codehaus.jackson.map.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0])
Hayi
  • 6,972
  • 26
  • 80
  • 139

3 Answers3

1

The all_stats list is an empty list, you need to check for that:

return ( all_stats.isEmpty() ? null : all_stats );
Gabriel J
  • 9
  • 2
  • no it's not a empty list , i found the answer here http://stackoverflow.com/questions/17400850/is-jackson-really-unable-to-deserialize-json-into-a-generic-type – Hayi Mar 25 '15 at 19:53
0

This is big hint "Could not write JSON: (was java.lang.NullPointerException"

Looks like something that you're passing along as is null. Are you initializing your map variable 'totalMap' before use?

Jigish
  • 1,764
  • 1
  • 15
  • 20
0

the answer was here so i edit my object lkike that :

public class StatAllBean<T> {

    public Map<T, Long> totalMap ;
    public Date date;


    @JsonCreator
    public StatAllBean(Date date , @JsonProperty("totalMap")  Map<T, Long> totalMap){
        this.date = date;
        this.totalMap = totalMap;

    }
    ...
Community
  • 1
  • 1
Hayi
  • 6,972
  • 26
  • 80
  • 139