NutzCN Logo
问答 ehcache集成,manager空指针
发布于 2719天前 作者 Cancerce1l 2219 次浏览 复制 上一个帖子 下一个帖子
标签:

cacheManager.getCache(cacheName) 报nullpointer;

报错信息:

java.lang.NullPointerException
	at com.well.utils.CacheUtil.getCache(CacheUtil.java:55)
	at com.well.utils.CacheUtil.put(CacheUtil.java:42)
	at com.well.utils.CacheUtil.put(CacheUtil.java:28)
	at com.well.wechat.handler.DefaultWxHandler.text(DefaultWxHandler.java:42)
	at org.nutz.weixin.util.Wxs.handle(Wxs.java:268)
	at org.nutz.weixin.impl.AbstractWxHandler.handle(AbstractWxHandler.java:93)
	at org.nutz.weixin.util.Wxs.handle(Wxs.java:699)
	at com.well.wechat.module.BindingModule.msgin(BindingModule.java:51)
	at com.well.wechat.module.BindingModule$FM$msgin$b806526ae658790edc3fc063949c9d61.invoke(BindingModule.java)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:31)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2536)
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2525)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:744)


js:
dao : {
            type : "org.nutz.dao.impl.NutDao",
            args : [{refer:"dataSource"}]
        },

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<ehcache updateCheck="false" name="defaultCache">

	<diskStore path="java.io.tmpdir/jeesite/ehcache/default" />

	<!-- DefaultCache setting. -->
	<defaultCache maxEntriesLocalHeap="100" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600"
		overflowToDisk="true" maxEntriesLocalDisk="100000" />
    	
</ehcache>

另外,不配置xml的,无法create吗?看到源码有create() 方法。

19 回复

DefaultWxHandler是new出来的?没放入ioc?

在ioc里,包括写的CacheUtil类。

@IocBean(create="init", name="wxHandler")
public class DefaultWxHandler extends BasicWxHandler {
@IocBean
public class CacheUtil {
	
	@Inject
	private static CacheManager cacheManager;

BindingModule 贴来看看

@At("/binding")
@IocBean
public class BindingModule extends BaseModule {
@Inject
    protected WxHandler wxHandler;

    @At // 拼起来的全路径就是 /weixin/msgin
    public View msgin(HttpServletRequest req) throws IOException {
        return Wxs.handle(wxHandler, req, "default"); // 最后面的default,可以不写,只是个标识符.
    }

有ehcache.js,对吧? 是不是没使用shiro? 贴出来看看

写在了dao.js里。

var ioc = {
        dataSource : {
            type : "com.alibaba.druid.pool.DruidDataSource",
            events : {
                create : "init",
                depose : 'close'
            },
            fields : {
                url : "jdbc:mysql://localhost:3306/socialp",
                username : "root",
                password : 1234567890,
                testWhileIdle : true, // 非常重要,预防mysql的8小时timeout问题
                //validationQuery : "select 1" , // Oracle的话需要改成 select 1 from dual
                maxActive : 100
            }
        },
        dao : {
            type : "org.nutz.dao.impl.NutDao",
            args : [{refer:"dataSource"}]
        },
        // 直接初始化Ehcache,默认找ehcache.xml文件哦
        cacheManager : {
            type : "net.sf.ehcache.CacheManager",
            factory : "net.sf.ehcache.CacheManager#create"
        }
        /* // 与shiro共享一个ehcache示例的方式
        cacheManager : {
            type : "net.sf.ehcache.CacheManager",
            factory : "net.sf.ehcache.CacheManager#getCacheManager",
            args : ["nutzbook"] // 对应shiro.ini中指定的ehcache.xml中定义的name
        }
         */
};

日志打出来的错误是这行。

Cache cache = cacheManager.getCache(cacheName);

奇怪了,你在MainSetup里,从ioc取一下CacheManager看看

会不会是xml配置的问题?

public class MainSetup implements Setup {

    public void init(NutConfig nc) {
    	
    	 Ioc ioc = nc.getIoc();
    	 CacheManager cacheManager= ioc.get(CacheManager.class);
    	 CacheUtil.get("list");
    }

    public void destroy(NutConfig nc) {
    }

}

2017-07-13 14:22:09,780 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:125) ERROR - Error happend during start serivce!
java.lang.NullPointerException
	at com.well.utils.CacheUtil.getCache(CacheUtil.java:55)
	at com.well.utils.CacheUtil.get(CacheUtil.java:36)
	at com.well.utils.CacheUtil.get(CacheUtil.java:24)
	at com.well.MainSetup.init(MainSetup.java:18)
	at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:274)
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:121)
	at org.nutz.mvc.ActionHandler.<init>(ActionHandler.java:19)
	at org.nutz.mvc.NutFilter._init(NutFilter.java:91)
	at org.nutz.mvc.NutFilter.init(NutFilter.java:69)
	at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
	at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
	at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105)
	at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4958)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5652)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:899)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
	at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1260)
	at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:2002)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
	at java.util.concurrent.FutureTask.run(FutureTask.java:262)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:744)

不太可能,检查一下cacheManager的定义,看看是不是多个js里面都配了,导致dao.js里面的没生效

啊,刚刚mainsetup里写错了。这样写,启动不报错。

public class MainSetup implements Setup {

    public void init(NutConfig nc) {
    	
    	 Ioc ioc = nc.getIoc();
    	 CacheManager cacheManager= ioc.get(CacheManager.class);
    	 Cache cache = cacheManager.getCache("syscache");
    }

    public void destroy(NutConfig nc) {
    }

}

加static属性会有影响吗?这是CacheUtil里的声明。

@Inject
	private static CacheManager cacheManager;

... 静态属性是不能注入的

啊.....那我改一下工具类。 (′д` )…彡…彡

再多问一句,是不是可以不配置xml,也能初始化?

ehcache.xml 吗?? 不加的话也有默认值

添加回复
请先登陆
回到顶部