NutzCN Logo
问答 按主见检索出现空指针异常
发布于 2672天前 作者 lk215316 2145 次浏览 复制 上一个帖子 下一个帖子
标签:

这个是用的ioc调取的Dao,这个查询没出问题

Cms_class cmsClass = null;
        cmsClass = ioc.get(Dao.class).fetch(Cms_class.class, 2L);

这个是按实体生成Service 在测试用类里面跑不通,主键用字符串和数字都不行

@Test
    public void test1() {
        Cms_class cmsClass = null;
        cmsClass = cmsClasssService.fetch(1l);
        Assert.assertNotNull(cmsClass);
        log.info(cmsClass.getId() + ", " + cmsClass.getTeacherName());
    }
java.lang.NullPointerException
	at cn.wizzer.test.cms.service.ClassService.test2(ClassService.java:57)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
13 回复

ClassService.test2(ClassService.java:57) 这一行的代码是什么?

 cmsClass = websiteGoodsService.fetch("40f061d1b0ee4bb888a48c0faa870a26");
cmsClass = cmsClasssService.fetch(1l);
cmsClass = cmsClasssService.fetch(1l);

cmsClasssService是null吧?

这个使用代码生成器生成出来的,

 @Inject
    private CmsClassService cmsClasssService;

在这个测试用类里面引入了

在Controller里面,如果主键是Long类型的话,也会报空指针,如果主键是String冷就没有这个问题,但是在测试用类里面,不论使用Long还是String类型的主键,都会出现空指针

那是两个问题

Controller里面Long报错是因为null值的Long转long会抛NPE,这是JVM的规则

测试类里面抛NPE,那是因为注入是有条件的 http://nutzam.com/core/testing/junit_testing.html

我打印过页面传过来的参数,Long类型的,不是null,还是抛异常了

贴报错信息

[WARN ] 2017-08-29 17:10:25,804 org.nutz.mvc.impl.processor.FailProcessor.process(FailProcessor.java:28) - Error@/platform/cms/classs/detail/1 :
org.nutz.dao.DaoException: Need @Name for class cn.wizzer.app.cms.modules.models.Cms_class
        at org.nutz.dao.impl.NutDao.fetch(NutDao.java:566)
        at cn.wizzer.framework.base.service.BaseServiceImpl.fetch(BaseServiceImpl.java:93)
        at cn.wizzer.app.web.modules.controllers.platform.cms.CmsClassController.detail(CmsClassController.java:110)
        at cn.wizzer.app.web.modules.controllers.platform.cms.CmsClassController$FM$detail$6052d1be34a69ad8029c9896542e7052.invoke(CmsClassController.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 cn.wizzer.app.web.commons.processor.XssSqlFilterProcessor.process(XssSqlFilterProcessor.java:35)
        at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
        at cn.wizzer.app.web.commons.processor.NutShiroProcessor.process(NutShiroProcessor.java:52)
        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.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
        at cn.wizzer.app.web.commons.processor.GlobalsSettingProcessor.process(GlobalsSettingProcessor.java:36)
        at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
        at cn.wizzer.app.web.commons.processor.LogTimeProcessor.process(LogTimeProcessor.java:21)
        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.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1621)
        at cn.wizzer.app.web.commons.filter.RouteFilter.doFilter(RouteFilter.java:31)
        at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1621)
        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.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1613)
        at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:541)
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
        at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
        at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:190)
        at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1592)
        at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
        at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1239)
        at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
        at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:481)
        at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1561)
        at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
        at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1141)
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
        at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:213)
        at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:118)
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
        at org.eclipse.jetty.server.Server.handle(Server.java:564)
        at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:320)
        at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
        at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
        at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)
        at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:672)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:590)
        at java.lang.Thread.run(Thread.java:745)

org.nutz.dao.DaoException: Need @Name for class cn.wizzer.app.cms.modules.models.Cms_class

你传的是字符串String,但这个类并没有@Name呢

厉害,Controller里面的问题解决了

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