NutzCN Logo
问答 druid连接池配置成功以后,空指针异常
发布于 2686天前 作者 qq_32ee9e89 5967 次浏览 复制 上一个帖子 下一个帖子
标签:

17-08-15 13:38:41.782 DEBUG [RMI TCP Connection(3)-127.0.0.1] loading [dao.js]
17-08-15 13:38:41.791 DEBUG [RMI TCP Connection(3)-127.0.0.1] Loaded 3 bean define from path=[ioc/] --> [conf, dao, dataSource]
tomcat启动显示 连接池启动成功
person = dao.fetch(Person.class,2);报空指针异常

@Inject
    protected Dao dao;

    @Inject
    private Person person;

    public void updatePerson(){
        person = dao.fetch(Person.class,2);
        person.setAge(100);
        dao.update(person);
        System.out.println("result");
    }
10 回复

上述代码所在的类, 不在ioc容器里面

IOC包名配置 com.aa 代码类所在包 com.aa.service 这么配置就不在容器里了吗?

对象要从ioc取出才算, 直接new是不行的

我没有new啊,通过注入拿到的Dao啊,如果配置成功了,不是直接注入拿到的就是从IOC容器取出的吗。

@IocBean
public class PersonService {

    @Inject
    protected Dao dao;

    @Inject
    private Person person;

    public void updatePerson(){
        person = dao.fetch(Person.class,2);
        person.setAge(100);
        dao.update(person);
        System.out.println("result");
    }

    public void savePerson(){

        // 创建表 false的含义是,如果表已经存在,就不要删除重建了.
        dao.create(Person.class, false);

        person = new Person();
        person.setName("ABCDEF");
        person.setAge(30);
        dao.insert(person);
        System.out.println(person.getId());
    }

    public void deletePerson(){

        person = dao.fetch(Person.class,1);
        dao.delete(person);
    }

哪里引用PersonService 的? 贴对应的类的代码

刚刚我看到我得PersonService是new的,现在改为注入的了,不报空指针异常,但是报了IOC异常。
异常信息:

17-08-15 14:02:28.692 DEBUG [http-nio-8080-exec-4] Found mapping for [GET] path=/say : PersonModule.doHello(PersonModule.java:29)
17-08-15 14:02:28.695 DEBUG [http-nio-8080-exec-4] Get 'personModule'<class com.kp.module.PersonModule>
17-08-15 14:02:28.696 DEBUG [http-nio-8080-exec-4] Get '$aop_async'<interface org.nutz.ioc.aop.config.AopConfigration>
17-08-15 14:02:28.698 DEBUG [http-nio-8080-exec-4] 	 >> Load definition name=$aop_async
17-08-15 14:02:28.704 DEBUG [http-nio-8080-exec-4] Found IocObject($aop_async) in AsyncAopIocLoader@1343867155
17-08-15 14:02:28.705 DEBUG [http-nio-8080-exec-4] 	 >> Make...'$aop_async'<interface org.nutz.ioc.aop.config.AopConfigration>
17-08-15 14:02:28.707 DEBUG [http-nio-8080-exec-4] Save object '$aop_async' to [app] 
17-08-15 14:02:28.738 DEBUG [http-nio-8080-exec-4] Load AopConfigure for anno=org.nutz.ioc.aop.Aop by type=org.nutz.ioc.aop.config.impl.AnnotationAopConfigration
17-08-15 14:02:28.738 DEBUG [http-nio-8080-exec-4] 	 >> Load definition name=personModule
17-08-15 14:02:28.739 DEBUG [http-nio-8080-exec-4] Found IocObject(personModule) in AnnotationIocLoader(packages=[com.kp])
17-08-15 14:02:28.739 DEBUG [http-nio-8080-exec-4] 	 >> Make...'personModule'<class com.kp.module.PersonModule>
17-08-15 14:02:28.740 DEBUG [http-nio-8080-exec-4] Load class com.kp.module.PersonModule without AOP
17-08-15 14:02:28.740 DEBUG [http-nio-8080-exec-4] Save object 'personModule' to [app] 
17-08-15 14:02:28.775 DEBUG [http-nio-8080-exec-4] Get 'personService'<class com.kp.service.PersonService>
17-08-15 14:02:28.776 DEBUG [http-nio-8080-exec-4] 	 >> Load definition name=personService
17-08-15 14:02:28.776 DEBUG [http-nio-8080-exec-4] Found IocObject(personService) in AnnotationIocLoader(packages=[com.kp])
17-08-15 14:02:28.776 DEBUG [http-nio-8080-exec-4] 	 >> Make...'personService'<class com.kp.service.PersonService>
17-08-15 14:02:28.776 DEBUG [http-nio-8080-exec-4] Load class com.kp.service.PersonService without AOP
17-08-15 14:02:28.776 DEBUG [http-nio-8080-exec-4] Save object 'personService' to [app] 
17-08-15 14:02:28.791 DEBUG [http-nio-8080-exec-4] Get 'dao'<interface org.nutz.dao.Dao>
17-08-15 14:02:28.791 DEBUG [http-nio-8080-exec-4] 	 >> Load definition name=dao
17-08-15 14:02:28.792 DEBUG [http-nio-8080-exec-4] Loading define for name=dao
17-08-15 14:02:28.801 DEBUG [http-nio-8080-exec-4] Found IocObject(dao) in JsonLoader(paths=[ioc/])
17-08-15 14:02:28.802 DEBUG [http-nio-8080-exec-4] 	 >> Make...'dao'<interface org.nutz.dao.Dao>
17-08-15 14:02:28.872 DEBUG [http-nio-8080-exec-4] Load class org.nutz.dao.impl.NutDao without AOP
17-08-15 14:02:28.873 DEBUG [http-nio-8080-exec-4] Save object 'dao' to [app] 
17-08-15 14:02:28.876 DEBUG [http-nio-8080-exec-4] Get 'dataSource'<>
17-08-15 14:02:28.876 DEBUG [http-nio-8080-exec-4] 	 >> Load definition name=dataSource
17-08-15 14:02:28.877 DEBUG [http-nio-8080-exec-4] Loading define for name=dataSource
17-08-15 14:02:28.928 DEBUG [http-nio-8080-exec-4] Found IocObject(dataSource) in JsonLoader(paths=[ioc/])
17-08-15 14:02:28.928 DEBUG [http-nio-8080-exec-4] 	 >> Make...'dataSource'<>
17-08-15 14:02:29.011 DEBUG [http-nio-8080-exec-4] Load class com.alibaba.druid.pool.DruidDataSource without AOP
17-08-15 14:02:29.011 DEBUG [http-nio-8080-exec-4] Save object 'dataSource' to [app] 
17-08-15 14:02:29.013 DEBUG [http-nio-8080-exec-4] Get 'conf'<>
17-08-15 14:02:29.014 DEBUG [http-nio-8080-exec-4] 	 >> Load definition name=conf
17-08-15 14:02:29.014 DEBUG [http-nio-8080-exec-4] Loading define for name=conf
17-08-15 14:02:29.050 DEBUG [http-nio-8080-exec-4] Found IocObject(conf) in JsonLoader(paths=[ioc/])
17-08-15 14:02:29.050 DEBUG [http-nio-8080-exec-4] 	 >> Make...'conf'<>
17-08-15 14:02:29.053 DEBUG [http-nio-8080-exec-4] Load class org.nutz.ioc.impl.PropertiesProxy without AOP
17-08-15 14:02:29.054 DEBUG [http-nio-8080-exec-4] Save object 'conf' to [app] 
17-08-15 14:02:29.196 DEBUG [http-nio-8080-exec-4] Found 1 resource by src( custom/ ) , regex( ^.+[.]properties$ )
17-08-15 14:02:29.196 DEBUG [http-nio-8080-exec-4] load properties from File[D:\worksearch\cloudsearch\manage\build\libs\exploded\manage-0.1.war\WEB-INF\classes\custom\db.properties]
[INFO ] 2017-08-15 14:02:31.046 [http-nio-8080-exec-4] DruidDataSource - {dataSource-1} inited
17-08-15 14:02:31.066 DEBUG [http-nio-8080-exec-4] Get 'dataSource'<>
17-08-15 14:02:31.546 DEBUG [http-nio-8080-exec-4] Jdbcs init complete
17-08-15 14:02:31.546 INFO [http-nio-8080-exec-4] Get Connection from DataSource for JdbcExpert, if you lock at here, check your database server and configure
Tue Aug 15 14:02:31 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
17-08-15 14:02:31.893 DEBUG [http-nio-8080-exec-4] select expert : org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert
17-08-15 14:02:31.902 DEBUG [http-nio-8080-exec-4] JDBC Driver --> mysql-connector-java-5.1.40 ( Revision: 402933ef52cad9aa82624e80acbea46e3a701ce6 )
17-08-15 14:02:31.902 DEBUG [http-nio-8080-exec-4] JDBC Name   --> MySQL Connector Java
17-08-15 14:02:31.902 DEBUG [http-nio-8080-exec-4] JDBC URL    --> jdbc:mysql://127.0.0.1:3306/nutzdemo
17-08-15 14:02:32.048 DEBUG [http-nio-8080-exec-4] Mysql : character_set_client=utf8
17-08-15 14:02:32.049 DEBUG [http-nio-8080-exec-4] Mysql : character_set_connection=utf8
17-08-15 14:02:32.049 DEBUG [http-nio-8080-exec-4] Mysql : character_set_database=utf8
17-08-15 14:02:32.049 DEBUG [http-nio-8080-exec-4] Mysql : character_set_filesystem=binary
17-08-15 14:02:32.050 DEBUG [http-nio-8080-exec-4] Mysql : character_set_results=
17-08-15 14:02:32.050 DEBUG [http-nio-8080-exec-4] Mysql : character_set_server=utf8
17-08-15 14:02:32.050 DEBUG [http-nio-8080-exec-4] Mysql : character_set_system=utf8
17-08-15 14:02:32.050 DEBUG [http-nio-8080-exec-4] Mysql : character_sets_dir=D:\tools\mysql\share\charsets\
17-08-15 14:02:32.056 DEBUG [http-nio-8080-exec-4] Mysql : binlog_format=ROW
17-08-15 14:02:32.058 DEBUG [http-nio-8080-exec-4] Mysql : database=nutzdemo
17-08-15 14:02:32.059 DEBUG [http-nio-8080-exec-4] Mysql : user=root@localhost
17-08-15 14:02:32.136 DEBUG [http-nio-8080-exec-4] Database info --> MYSQL:[MySQL - 5.7.17-log]
17-08-15 14:02:32.142 DEBUG [http-nio-8080-exec-4] name=person not found, search for type=com.kp.entity.Person
17-08-15 14:02:32.143 DEBUG [http-nio-8080-exec-4] Loading define for name=conf
17-08-15 14:02:32.143 DEBUG [http-nio-8080-exec-4] Found IocObject(conf) in JsonLoader(paths=[ioc/])
17-08-15 14:02:32.143 DEBUG [http-nio-8080-exec-4] Loading define for name=dao
17-08-15 14:02:32.144 DEBUG [http-nio-8080-exec-4] Found IocObject(dao) in JsonLoader(paths=[ioc/])
17-08-15 14:02:32.144 DEBUG [http-nio-8080-exec-4] Loading define for name=dataSource
17-08-15 14:02:32.144 DEBUG [http-nio-8080-exec-4] Found IocObject(dataSource) in JsonLoader(paths=[ioc/])
17-08-15 14:02:32.145 DEBUG [http-nio-8080-exec-4] Found IocObject(personModule) in AnnotationIocLoader(packages=[com.kp])
17-08-15 14:02:32.145 DEBUG [http-nio-8080-exec-4] Found IocObject(personService) in AnnotationIocLoader(packages=[com.kp])
17-08-15 14:02:32.145 DEBUG [http-nio-8080-exec-4] Loading define for name=txREPEATABLE_READ
17-08-15 14:02:32.146 DEBUG [http-nio-8080-exec-4] Found IocObject(txREPEATABLE_READ) in TransIocLoader@1351646421
17-08-15 14:02:32.146 DEBUG [http-nio-8080-exec-4] Loading define for name=txSERIALIZABLE
17-08-15 14:02:32.147 DEBUG [http-nio-8080-exec-4] Found IocObject(txSERIALIZABLE) in TransIocLoader@1351646421
17-08-15 14:02:32.147 DEBUG [http-nio-8080-exec-4] Loading define for name=txNONE
17-08-15 14:02:32.147 DEBUG [http-nio-8080-exec-4] Found IocObject(txNONE) in TransIocLoader@1351646421
17-08-15 14:02:32.147 DEBUG [http-nio-8080-exec-4] Loading define for name=txREAD_UNCOMMITTED
17-08-15 14:02:32.147 DEBUG [http-nio-8080-exec-4] Found IocObject(txREAD_UNCOMMITTED) in TransIocLoader@1351646421
17-08-15 14:02:32.147 DEBUG [http-nio-8080-exec-4] Loading define for name=txREAD_COMMITTED
17-08-15 14:02:32.148 DEBUG [http-nio-8080-exec-4] Found IocObject(txREAD_COMMITTED) in TransIocLoader@1351646421
17-08-15 14:02:32.148 DEBUG [http-nio-8080-exec-4] Found IocObject($aop_async) in AsyncAopIocLoader@1343867155
17-08-15 14:02:32.148 DEBUG [http-nio-8080-exec-4] Remove object 'personService' from [app] 
17-08-15 14:02:32.149 DEBUG [http-nio-8080-exec-4] Remove object 'personModule' from [app] 
17-08-15 14:02:32.149 WARN [http-nio-8080-exec-4] Error@/say :
org.nutz.ioc.IocException: IocBean[personModule] throw Exception when creating
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:149)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:116)
	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:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
	at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
	at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:796)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1366)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: IocBean[personModule] fail at field=[personService]
	at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:40)
	at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:56)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:135)
	... 44 more
Caused by: org.nutz.ioc.IocException: IocBean[personService] throw Exception when creating
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:149)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
	at org.nutz.ioc.val.ReferTypeValue.get(ReferTypeValue.java:60)
	at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:32)
	... 46 more
Caused by: java.lang.RuntimeException: IocBean[personService] fail at field=[person]
	at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:40)
	at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:56)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:135)
	... 49 more
Caused by: org.nutz.ioc.IocException: IocBean[class:com.kK.entity.Person] none ioc bean match class=com.kK.entity.Person
	at org.nutz.ioc.impl.NutIoc.getByType(NutIoc.java:399)
	at org.nutz.ioc.val.ReferTypeValue.get(ReferTypeValue.java:66)
	at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:32)
	... 51 more

PersonModule

@IocBean
public class PersonModule {

    @Inject
    private PersonService personService;

    @Filters
    @At("/say")
    @Ok("jsp:jsp.nutzTest")
    @Fail("json")
    public String doHello(HttpServletRequest request){
        personService.updatePerson();
        return "Hello Nutz";
    }
}

问题已经自己解决,调用的Person类没有加IOCBean注解,麻烦您了

Person类是个pojo吧? 怎么扯上ioc...

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