spring-attic / spring-social-twitter

Twitter API binding and connect support.
http://projects.spring.io/spring-social-twitter
Apache License 2.0
122 stars 115 forks source link

Take into account that a tweet's lang is nullable #103

Closed ptamarit closed 8 years ago

ptamarit commented 8 years ago

Hi,

I recently had a few stacktraces like this when retrieving users' most recent tweets:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: (was java.lang.NullPointerException) (through reference chain: org.springframework.social.twitter.api.impl.TweetList[89]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: org.springframework.social.twitter.api.impl.TweetList[89])
  at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:229)
  at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:213)
  at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:95)
  at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:599)
  at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:572)
  at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:280)
  at org.springframework.social.twitter.api.impl.TimelineTemplate.getUserTimeline(TimelineTemplate.java:107)
  at org.springframework.social.twitter.api.impl.TimelineTemplate.getUserTimeline(TimelineTemplate.java:99)
  [...]
Caused by: com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: org.springframework.social.twitter.api.impl.TweetList[89])
  at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:210)
  at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:189)
  at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:266)
  at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
  at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
  at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3736)
  at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2810)
  at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:226)
  ... 24 more
Caused by: java.lang.NullPointerException
  at org.springframework.social.twitter.api.impl.TweetDeserializer.deserialize(TweetDeserializer.java:75)
  at org.springframework.social.twitter.api.impl.TweetDeserializer.deserialize(TweetDeserializer.java:53)
  at org.springframework.social.twitter.api.impl.TweetDeserializer.deserialize(TweetDeserializer.java:45)
  at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
  ... 29 more

Turns out that a tweet's lang is nullable according to the docs.

In my case the JsonNode was null (hence the NPE) but I also added a check for JsonNode#isNull. Without this last check, NullNode#asText would return the String "null", so I thought it would probably be cleaner to return a real null.

I signed the CLA with the same email address as the one used in the commit.

Cheers, Pablo