Home > OS >  Failed to execute CommandLineRunner - Spring Boot
Failed to execute CommandLineRunner - Spring Boot

Time:10-15

I am new to Spring and am dealing with this problem.

It throws an exception:

Failure when trying to execute CommandLineRunner.

I have tried several methods, but the result in console is still the same:

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 `Datos en Consola`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

Code:

@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");
        };

    }

}

CodePudding user response:

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]

So the problem is in line 36 of UserServiceImpl.java. My guess: due to the first parameter of that method: addRoleToUser

... Probably just a typo: "john" vs. "jhon"

  • Related