java redis 问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java redis 问题相关的知识,希望对你有一定的参考价值。

代码如下:
public String getInfo(String infoName)
try
Jedis jedis = jedisPool.getResource();
try
String key = infoName+":" + infoName;
byte[] bytes = jedis.get(key.getBytes());
if (bytes != null)
String info = schema.newMessage();
ProtostuffIOUtil.mergeFrom(bytes, info, schema);
return info;

finally
jedis.close();

catch (java.lang.Exception e)
logger.error(e.getMessage(), e);

return null;


public String putInfo(String infoName,String info)
try
Jedis jedis = jedisPool.getResource();
try
String key = infoName+":" + info;
byte[] bytes = ProtostuffIOUtil.toByteArray(info, schema, LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
int timeout = 60 * 60;
String result=jedis.setex(key.getBytes(), timeout, bytes);
return result;
finally
jedis.close();

catch (Exception e)
logger.error(e.getMessage(), e);

return null;


public long getTotalPage()
if (redisDao.getInfo("totalPage") == null)
long allBlogCount = blogDao.getCount();
System.out.println("allBlogCount..............:"+allBlogCount);
redisDao.putInfo("totalPage", (allBlogCount / 8 + 1) + "");
return allBlogCount / 8 + 1;
else
System.out.println("totalPage..............: "+redisDao.getInfo("totalPage"));
return Long.getLong(redisDao.getInfo("totalPage"));



@RequestMapping("/")
public String home(Model model)
List<Blog> blogs = blogServiceImpl.queryAll(0, 0);
model.addAttribute("blogs", blogs);
model.addAttribute("categorys",categoryServiceImpl.queryAll());
model.addAttribute("pageIndex", 1);
System.out.println(redisDao.getInfo("totalPage")+" controller totalPage ");
model.addAttribute("totalPage",blogServiceImpl.getTotalPage()); // redisDao.getInfo("totalPage") 这行出错
return "/home";

提示:
org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [seckill-dispatcher] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException

参考技术A 错误很明显是空指针异常,点个debug运行,跟踪一下,一遍就能找出是哪个为空。redisDao.getInfo("totalPage")这句为空的话,很大可能是redisDao 是null,可以跟踪代码看一下,是不是没有实例化这个对象。 参考技术B redisDao是空的,看看有没有实例化

以上是关于java redis 问题的主要内容,如果未能解决你的问题,请参考以下文章

redis集群角色切换java调用异常

Java开发中遇到最难的问题!最全Java面试知识点梳理

java连接redis问题

JAVA面试常见问题之Redis篇

java连接redis,实现订阅发布遇到的问题

java操作redis集群问题CLUSTERDOWN The cluster is down. Use CLUSTER INFO for more information