NutzCN Logo
问答 为什么事物关闭时报空指针异常,我只关闭一次,但日志好像关了两次。
发布于 2350天前 作者 qq_93105859 1570 次浏览 复制 上一个帖子 下一个帖子
标签:
日志:
java.lang.NullPointerException
	at org.nutz.trans.Trans._depose(Trans.java:93)
	at org.nutz.trans.Trans.close(Trans.java:255)
	at com.lb.sw.service.impl.wx.buyer.WxSmallProgramBuyerUserServiceImpl.login(WxSmallProgramBuyerUserServiceImpl.java:182)
	at com.lb.sw.module.wx.buyer.WxSmallProgramBuyerUserModule.login(WxSmallProgramBuyerUserModule.java:46)
	at com.lb.sw.module.wx.buyer.WxSmallProgramBuyerUserModule$FM$login$bad5a2af1bb3aefbecb8706d61f37768.invoke(WxSmallProgramBuyerUserModule.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:38)
	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 com.lb.sw.mvc.LogTimeProcessor.process(LogTimeProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at com.lb.sw.mvc.CheckSessionProcessor.process(CheckSessionProcessor.java:38)
	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 com.lb.sw.web.filter.SwFilter.doFilter(SwFilter.java:49)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
	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:496)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
	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:803)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
	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)
2018-07-16 18:12:35,153 [DEBUG-https-jsse-nio-443-exec-8:33829351] com.lb.sw.mvc.LogTimeProcessor.process(LogTimeProcessor.java:31) 

代码:

public WxReturns login(String ip, WxReq req) throws Exception {
		try {
			String code = req.getCode();
			if (!StringUtils.compareTrim(code)) {
				String msg = "缺少必要参数:code=" + code;
				log.info(msg);
				return WxReturns.me(false, msg);
			} else {
				String json = WxUtils.jscode2session(Config.APP_ID_SHOP, Config.APP_SECRET_SHOP, code);
				if (!StringUtils.compareTrim(json)) {
					log.info("网络超时!");
					return WxReturns.me(false, "网络超时!");
				} else {
					WxServerResponse code2sessionResp = Json.fromJson(WxServerResponse.class, json);
					if (code2sessionResp.getErrcode() > 0) {
						String errorMsg = "小程序code换取session_key返回的错误码:" + code2sessionResp.getErrcode();
						log.debug(errorMsg);
						return WxReturns.me(false, errorMsg);
					} else if (StringUtils.compareTrim(code2sessionResp.getSession_key())) {
						Trans.begin();
						log.info("调用微信接口成功!");
						String openid = code2sessionResp.getOpenid();
						BaseWx wx = dao.fetch(BaseWx.class, Cnd.where("openId", "=", code2sessionResp.getOpenid()));
						Session session = SC.create();
						session.setSessionType(Session.SESSIONTYPE_WECHAT);
						BaseUser user = null;
						Date nowDat = DateUtils.date();
						if (wx == null) {
							user = new BaseUser();
							String nickName = StringUtils.filterEmoji(req.getNickName());// 替换昵称中的4字节字符
							user.setName(nickName);
							// private Integer sex;//性别
							user.setSex(coverWxSex(req.getGender()));
							// private String mobile;//手机号码
							// private String uName;//用户名
							user.setDeleted(0);
							user.setEnableLogin(BaseUser.ENABLELOGIN_YES);
							user.setLoginCount(1L);
							user.setLastLoginTime(nowDat);
							user.setBalance(0.0);
							user.setDepositTotal(0.0);
							user.setDistributorFlag(BaseUser.DISTRIBUTORFLAG_NO);
							user.setCreateTime(nowDat);
							user.setExpenseFee(0.0);
							user.setInvitationCount(0);
							dao.insert(user);
							baseUserService.referralCodeBuilder(user);// 生成用户邀请码
							wx = new BaseWx();
							Long userPk = user.getUserPk();
							wx.setUserPk(userPk);
							BaseVipRanks defaultRanks = baseVipRanksService.defaultRanks();// 得到默认的会员等级
							if (defaultRanks != null)
								wx.setVrPk(defaultRanks.getVrPk());
							wx.setOpenId(openid);
							String unionid = WxUtils.getUnionId(Config.APP_ID_SHOP, Config.APP_SECRET_SHOP, openid);
							wx.setUnionId(unionid);
							wx.setBindTime(nowDat);
							wx.setNickName(nickName);
							wx.setAvatarUrl(req.getAvatarUrl());
							wx.setGender(req.getGender());
							wx.setProvince(req.getProvince());
							wx.setCity(req.getCity());
							wx.setCountry(req.getCountry());
							wx.setAppType(BaseWx.APP_TYPE_BUYER);
							wx.setIsBlacklist(false);
							dao.insert(wx);
							// 如果客户属于扫描小程序二维码进来的则将改用户与对应水站绑定
							long bwsPk = req.getBwsPk();
							if (bwsPk > 0L)
								bindStations(bwsPk, userPk);
							// 发放并领用优惠券
							List<PrmSendDetails> sents = prmSendDetailsService.autoSentRegCoupon();
							prmSendDetailsService.takeCoupons(sents, wx.getWxPk());
						} else {
							if (wx.getIsBlacklist())
								return WxReturns.me(false, WxReturns.CODE_HMDHY);
							user = dao.fetch(BaseUser.class, wx.getUserPk());
							Long loginCount = user.getLoginCount();
							user.setLoginCount(loginCount == null ? 1L : ++loginCount);
							user.setLastLoginTime(nowDat);
						}
						session.setAttribute(AK.S_USER, user);
						session.setAttribute(AK.S_WECHAT, wx);
						WxReturns returns = WxReturns.me();
						WxLogin wl = new WxLogin();
						wl.setSessionId(session.getSessionId());
						wl.setUser(user);
						// 获取会员等级数据
						Long vrPk = wx.getVrPk();
						if (vrPk != null && vrPk != 0L) {
							BaseVipRanks ranks = dao.fetch(BaseVipRanks.class, wx.getVrPk());
							session.setAttribute(AK.S_RANKS, ranks);
							wl.setRankName(ranks.getRankName());// 会员等级名称
							wl.setDiscountsFee(ranks.getDiscountsFee());// 会员享受优惠的金额
						}
						returns.setObj(wl);
						logLogin(user.getUserPk(), ip, user.getuName(), LogLogin.RESULT_SUCC, "");
						Trans.commit();
						return returns;
					}
					logLogin(0L, ip, "", LogLogin.RESULT_FARL, "发生未知的错误");
					return WxReturns.me(false, WxReturns.CODE_WZCW);
				}
			}
		} catch (Exception e) {
			Trans.rollback();
			throw e;
		} finally {
			Trans.close();
		}
	}
8 回复

不小心把代码全部提交忘了删减掉那些不重要的部分了,又没找到地方修改。不好意思。

begin了才能close, 代码里面有些分支是匹配不上的

我前面不是有调用begin方法吗?而且这个空指针的异常是偶发的,不是每次都会报空指针。我找过,但找不到问题。

没走begin的时候就肯定死啦

何必这样写呢, 加个@Aop(TransAop.XXX) 完事

还真是,我居然没发现这个问题。@Aop(TransAop.XXX)这个具体要怎么加呢,请教兽总

好了,谢谢兽总。

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