请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

老师好,报了空指针异常,一直找不到原因

这个是我根据课程里的微信账号的创建修改的一个注册功能,我也是先插入用户表再插入账号,密码,但是还是报空指针异常,DAO层测试都通过了,这个到底是啥原因,老师支个招吧,我弄了好久都没有弄好,太惨了。
这个是LocalAuthServiceImpl的代码
public LocalAuthExecution registeLocalAuth(LocalAuth localAuth) throws LocalAuthOperationException {
// 空值判断
if (localAuth == null || localAuth.getPersonInfo() == null) {
return new LocalAuthExecution(LocalAuthStateEnum.NULL_AUTH_INFO);
}
try {
// 设置创建时间
localAuth.setCreateTime(new Date());
// 如果平台帐号里夹带着用户信息并且用户Id为空,则认为该用户注册账号
// 则自动创建用户信息
if (localAuth.getPersonInfo() != null && localAuth.getPersonInfo().getUserId() == null) {
try {
localAuth.getPersonInfo().setCreateTime(new Date());
localAuth.getPersonInfo().setEnableStatus(1);
PersonInfo personInfo = localAuth.getPersonInfo();
int effectedNum = personInfoDao.insertPersonInfo(personInfo);
localAuth.setPersonInfo(personInfo);
if (effectedNum <= 0) {
throw new LocalAuthOperationException(“添加用户信息失败”);
}
} catch (Exception e) {
log.error(“insertPersonInfo error:” + e.toString());
throw new LocalAuthOperationException("insertPersonInfo error: " + e.getMessage());
}
}
// 注册平台帐号
int effectedNum = localAuthDao.insertLocalAuth(localAuth);
if (effectedNum <= 0) {
throw new LocalAuthOperationException(“注册帐号失败”);
} else {
return new LocalAuthExecution(LocalAuthStateEnum.SUCCESS, localAuth);
}
} catch (Exception e) {
log.error(“insertLocalAuth error:” + e.toString());
throw new LocalAuthOperationException("insertLocalAuth error: " + e.getMessage());
}
}
这个是测试的代码
@Test
public void testABindLocalAuth() {
LocalAuth localAuth = new LocalAuth();
PersonInfo personInfo = new PersonInfo();
String username = “testusername”;
String password = “testpassword”;
// 给平台帐号设置上用户信息
personInfo.setName(“测试一下”);
personInfo.setGender(“女”);
personInfo.setUserType(2);
// 给平台帐号设置用户信息,标明是与哪个用户绑定
localAuth.setPersonInfo(personInfo);
// 设置帐号
localAuth.setUsername(username);
// 设置密码
localAuth.setPassword(password);
// 注册帐号
LocalAuthExecution lae = localAuthService.registeLocalAuth(localAuth);
assertEquals(PersonInfoStateEnum.SUCCESS.getState(), lae.getState());
// 通过userId找到新增的localAuth
localAuth = localAuthService.getLocalAuthByUserId(personInfo.getUserId());
// 打印用户名字和帐号密码看看跟预期是否相符
System.out.println(“用户昵称:” + localAuth.getPersonInfo().getName());
System.out.println(“平台帐号密码:” + localAuth.getPassword());
}
报错信息:
java.lang.NullPointerException
at com.qsf.bysj.service.LocalAuthServiceTest.testABindLocalAuth(LocalAuthServiceTest.java:45)
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.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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

45行是这里:LocalAuthExecution lae = localAuthService.registeLocalAuth(localAuth);

正在回答 回答被采纳积分+3

1回答

翔仔 2019-10-02 21:46:05

同学好,直接看代码很难看出问题的,何况你这边没有按照视频来。。需要你设置断点一步步去跟踪调试呢。。看看registeLocalAuth方法里面到底哪里抛出了空指针,为什么会抛出。此外,这个还远没到惨的地步,工作中遇到问题一个月解决不了还没人帮,还有压力才叫惨。。所以一定要锻炼出解决问题的能力

0 回复 有任何疑惑可以回复我~
  • 提问者 qq_独闭关中_0 #1
    老师好,问题我知道出在哪了,中文乱码问题,前端获取还是正常的,但是传到后端的时候就变成乱码了,再回看之前的视频,也是这样获取的呀,只不过我没有打包了一起传,一起接收,我是一个个传,后端一个个接收。网上各种方法都试过了,还是不行,不用中午测试是正常了的。
    回复 有任何疑惑可以回复我~ 2019-10-03 16:58:06
  • 翔仔 回复 提问者 qq_独闭关中_0 #2
    "不用中午测试是正常了的"是啥意思啊,一个个接收可能编码就是不一样,建议检查前后端编码,乱码肯定是前后编码不一致造成的
    回复 有任何疑惑可以回复我~ 2019-10-04 00:25:22
  • 提问者 qq_独闭关中_0 回复 翔仔 #3
    写错了,是中文,用户名用中文的话到后端接收就成乱码了,就插不进表,就没有user_id,就报了空指针异常,用户名换成字母就能注册成功了,所以就参考老师之前注册店铺,添加商品等中文传值方式。
    回复 有任何疑惑可以回复我~ 2019-10-04 17:07:27
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信