蓝天

java thrift返回List异常

对于下段代码:
public List<String> hmget(String key, List<String> fields) throws org.apache.thrift.TException
{
    JedisCluster jedis_cluster = RedisClusterProxy.get_jedis_cluster();	
    String[] field_array = new String[fields.size()];
    fields.toArray(field_array);
    return jedis_cluster.hmget(key, field_array);
}  



运行时遇到如下异常,原因是由于hmget返回的List<String>含有null成员,导致thrift编码时异常:
20160415 14:55:39 ERROR org.apache.thrift.server.AbstractNonblockingServer$FrameBuffer.invoke(AbstractNonblockingServer.java:484) [Thread-0] Unexpected throwable while invoking!
java.lang.NullPointerException
        at org.apache.thrift.protocol.TBinaryProtocol.writeString(TBinaryProtocol.java:185)
        at com.test.redis_cluster_proxy.RedisClusterProxyService$hmget_result$hmget_resultStandardScheme.write(RedisClusterProxyService.java:19434)
        at com.test.redis_cluster_proxy.RedisClusterProxyService$hmget_result$hmget_resultStandardScheme.write(RedisClusterProxyService.java:1)
        at com.test.redis_cluster_proxy.RedisClusterProxyService$hmget_result.write(RedisClusterProxyService.java:19337)
        at org.apache.thrift.ProcessFunction.process(ProcessFunction.java:53)
        at org.apache.thrift.TBaseProcessor.process(TBaseProcessor.java:39)
        at org.apache.thrift.server.AbstractNonblockingServer$FrameBuffer.invoke(AbstractNonblockingServer.java:478)
        at org.apache.thrift.server.TNonblockingServer.requestInvoke(TNonblockingServer.java:115)
        at org.apache.thrift.server.AbstractNonblockingServer$AbstractSelectThread.handleRead(AbstractNonblockingServer.java:209)
        at org.apache.thrift.server.TNonblockingServer$SelectAcceptThread.select(TNonblockingServer.java:198)
        at org.apache.thrift.server.TNonblockingServer$SelectAcceptThread.run(TNonblockingServer.java:154)
        
当redis中没有相应的field时,hmget返回的List<String>会包含null成员。解决此问题有两个办法:
1)保证查询的field一定存在
2)对hmget返回值做处理,null成员替换成空字符串""
     

posted on 2016-04-15 15:28  #蓝天  阅读(760)  评论(0编辑  收藏  举报

导航