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

来源:10-8 微信登录帐号的创建中

qq_独闭关中_0

2019-10-02

这个是我根据课程里的微信账号的创建修改的一个注册功能,我也是先插入用户表再插入账号,密码,但是还是报空指针异常,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);

写回答

1回答

翔仔

2019-10-02

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

0
5
qq_独闭关中_0
回复
翔仔
感谢老师!已经解决了,参考了修改密码的传值方式, var formData = new FormData(); formData.append('userName', userName); formData.append('password', password); formData.append('newPassword', newPassword); 再将formData传到后端,这样后端接收就不是乱码了,这个是将获取到的表单数据转成二进制吧。如果是像绑定账号那样直接获取表单数据,一个个传到后端,后端接收中文就成乱码了。
2019-10-05
共5条回复

Java双版本(SSM到SpringBoot)校园商铺全栈开发

SSM商铺V1.0,解决毕设痛点;SpringBoot商铺V2.0,满足工作刚需

5113 学习 · 8144 问题

查看课程