NutzCN Logo
问答 **Module.java 引用别的 **Module.java的非action方法的时候
发布于 241天前 作者 zjhk 474 次浏览 复制 上一个帖子 下一个帖子
标签:

AModule.java


import com.hk.entitys.hegui.Answer; import org.nutz.lang.util.NutMap; import org.nutz.log.Log; import org.nutz.log.Logs; import org.nutz.mvc.annotation.At; import org.nutz.mvc.annotation.Ok; import javax.servlet.http.HttpServletRequest; import java.sql.SQLException; @At("/a") public class AModule extends BaseModule { private static final Log log = Logs.get(); @Ok("json") @At("/test") public Object testA(HttpServletRequest request) throws SQLException { NutMap re = new NutMap().setv("ok", false); Integer questionId = 72; BModule bModule = new BModule(); Answer answer = bModule.queryAnswers(questionId).get(0); return re.setv("ok", true).setv("data", answer).setv("msg", "查询成功"); } }

BModule.java

import com.hefupb.hegui.entitys.hegui.Answer;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import java.sql.SQLException;
import java.util.List;
@At("/b")
public class BModule extends BaseModule {
    private static final Log log = Logs.get();
    public List<Answer> queryAnswers(Integer questionId) throws SQLException {
        log.debug("questionId: " + questionId);
        Sql sql = Sqls.create("SELECT * FROM t_answer WHERE id = @questionId");
        sql.params().set("questionId", questionId);
        sql.setCallback(Sqls.callback.entities());
        log.debug("sql: "+sql);
        sql.setEntity(dao.getEntity(Answer.class));
        dao.execute(sql);
        return sql.getList(Answer.class);
    }
}

错误信息:

2018-01-22 12:52:35,169 [http-nio-8081-exec-4] DEBUG org.nutz.mvc.impl.UrlMappingImpl - Found mapping for [GET] path=/a/test : AModule.testA(AModule.java:22)
2018-01-22 12:52:35,177 [http-nio-8081-exec-4] DEBUG org.nutz.ioc.impl.NutIoc - Get 'aModule'<class com.hefupb.hegui.modules.AModule>
2018-01-22 12:52:35,178 [http-nio-8081-exec-4] DEBUG org.nutz.ioc.impl.NutIoc - 	 >> Load definition name=aModule
2018-01-22 12:52:35,178 [http-nio-8081-exec-4] DEBUG org.nutz.ioc.loader.combo.ComboIocLoader - Found IocObject(aModule) in AnnotationIocLoader(packages=[com.hefupb.hegui.modules])
2018-01-22 12:52:35,178 [http-nio-8081-exec-4] DEBUG org.nutz.ioc.impl.NutIoc - 	 >> Make...'aModule'<class com.hefupb.hegui.modules.AModule>
2018-01-22 12:52:35,179 [http-nio-8081-exec-4] DEBUG org.nutz.ioc.impl.ScopeContext - Save object 'aModule' to [app] 
2018-01-22 12:52:35,179 [http-nio-8081-exec-4] DEBUG org.nutz.ioc.aop.impl.DefaultMirrorFactory - Load class com.hefupb.hegui.modules.AModule without AOP
2018-01-22 12:52:35,184 [http-nio-8081-exec-4] DEBUG org.nutz.ioc.impl.NutIoc - Get 'dao'<>
2018-01-22 12:52:35,184 [http-nio-8081-exec-4] DEBUG com.hefupb.hegui.modules.BModule - questionId: 72
2018-01-22 12:52:35,248 [http-nio-8081-exec-4] DEBUG com.hefupb.hegui.modules.BModule - sql: SELECT * FROM t_answer WHERE id = 72
2018-01-22 12:52:35,250 [http-nio-8081-exec-4] WARN  org.nutz.mvc.impl.processor.FailProcessor - Error@/a/test :
java.lang.NullPointerException
	at com.hefupb.hegui.modules.BModule.queryAnswers(BModule.java:20)
	at com.hefupb.hegui.modules.AModule.testA(AModule.java:25)
	at com.hefupb.hegui.modules.AModule$FM$testA$cdaa9d49c1fdb69706b49b81bfece3a4.invoke(AModule.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:30)
	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:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
	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:650)
	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:799)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1457)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

11 回复

**Module.java 引用别的 **Module.java的非action方法的时候,会报NullPointerException

dao是null?

@wendal 将BModule.java 里面的queryAnswers()方法放在AModule.java下面就不会报上述错误!不知道什么原因导致的?

@wendal 怎么调用别的Module里面非action的方法?

BModule.java:20 是哪一行,什么为null了

@wendal sql.setEntity(dao.getEntity(Answer.class));

父类和当前类都写了dao属性?

只在BaseModule.java 里面注入了Dao,其他的Module都是继承BaseModule的

@IocBean
public abstract class BaseModule {

    @Inject("refer:dao")
    protected Dao dao;
}

不能mew啊,要注入啊

BModule bModule = new BModule();

另外,这种调用不好的,应该抽象到service类

@wendal 好的,谢谢!

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