Spring Boot执行CommandLineRunner失败问题求助
Spring Boot CommandLineRunner执行失败问题解决
Spring Boot项目执行CommandLineRunner时抛出Failed to execute CommandLineRunner异常,控制台错误日志如下:
java.lang.IllegalStateException: Failed to execute CommandLineRunner at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:774) ~[spring-boot-2.7.4.jar:2.7.4] at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:755) ~[spring-boot-2.7.4.jar:2.7.4] at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.7.4.jar:2.7.4] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.7.4.jar:2.7.4] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-2.7.4.jar:2.7.4] at io.getarrays.userservice.UserserviceApplication.main(UserserviceApplication.java:19) ~[classes/:na] Caused by: java.lang.NullPointerException: Cannot invoke "io.getarrays.userservice.domain.User.getRoles()" because "user" is null at io.getarrays.userservice.service.UserServiceImpl.addRoleToUser(UserServiceImpl.java:36) ~[classes/:na] at io.getarrays.userservice.service.UserServiceImpl$$FastClassBySpringCGLIB$$cf86a147.invoke(<generated>) ~[classes/:na] at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.3.23.jar:5.3.23] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:793) ~[spring-aop-5.3.23.jar:5.3.23] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.3.23.jar:5.3.23] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) ~[spring-aop-5.3.23.jar:5.3.23] at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-5.3.23.jar:5.3.23] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) ~[spring-tx-5.3.23.jar:5.3.23] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.23.jar:5.3.23] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.23.jar:5.3.23] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) ~[spring-aop-5.3.23.jar:5.3.23] at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:708) ~[spring-aop-5.3.23.jar:5.3.23] at io.getarrays.userservice.service.UserServiceImpl$$EnhancerBySpringCGLIB$$4c838cf9.addRoleToUser(<generated>) ~[classes/:na] at io.getarrays.userservice.UserserviceApplication.lambda$run$0(UserserviceApplication.java:35) ~[classes/:na] at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:771) ~[spring-boot-2.7.4.jar:2.7.4] ... 5 common frames omitted
相关代码:
@SpringBootApplication public class UserserviceApplication { public static void main(String[] args) { SpringApplication.run(UserserviceApplication.class, args); } @Bean CommandLineRunner run(UserService userService){ return args -> { // Save Role userService.saveRole(new Role(null, "ROLE_USER")); userService.saveRole(new Role(null, "ROLE_MANAGER")); userService.saveRole(new Role(null, "ROLE_ADMIN")); userService.saveRole(new Role(null, "ROLE_SUPER_ADMIN")); // Save User userService.saveUser(new User(null,"John Travolta","jhon","1234", new ArrayList<>())); userService.saveUser(new User(null,"Will Smith","will","1234", new ArrayList<>())); userService.saveUser(new User(null,"Jim Carry","jim","1234", new ArrayList<>())); userService.saveUser(new User(null,"Arnold Schwarzenegger","arnold","1234", new ArrayList<>())); // Add role to user userService.addRoleToUser("john","ROLE_USER"); userService.addRoleToUser("will","ROLE_MANAGER"); userService.addRoleToUser("jim","ROLE_ADMIN"); userService.addRoleToUser("arnold","ROLE_SUPER_ADMIN"); userService.addRoleToUser("arnold","ROLE_ADMIN"); userService.addRoleToUser("arnold","ROLE_USER"); }; } }
问题分析
核心异常为java.lang.NullPointerException: Cannot invoke "io.getarrays.userservice.domain.User.getRoles()" because "user" is null,本质原因是:
- 保存第一个用户时,用户名写为
jhon - 后续添加角色时,使用的用户名是
john - 拼写不一致导致
userService.addRoleToUser方法查询不到用户,返回null,调用user.getRoles()时触发空指针。
解决方案
1. 修正用户名拼写错误
将保存用户的代码中,第一个用户的用户名从jhon改为john,保持与添加角色时一致:
userService.saveUser(new User(null,"John Travolta","john","1234", new ArrayList<>()));
2. 增强服务层空指针防护
在UserServiceImpl的addRoleToUser方法中增加空判断,提前抛出明确异常,避免空指针:
public void addRoleToUser(String username, String roleName) { User user = userRepository.findByUsername(username); if (user == null) { throw new RuntimeException("用户不存在:" + username); } Role role = roleRepository.findByName(roleName); if (role == null) { throw new RuntimeException("角色不存在:" + roleName); } user.getRoles().add(role); userRepository.save(user); }
内容的提问来源于stack exchange,提问作者Angello G_De la torre
相关产品推荐
相关产品推荐

