0

When I added java-melody starter dependency on my spring boot application, it throws null pointer exception whenever i try to access the application.

I added this dependency in my pom file

    <dependency>
            <groupId>net.bull.javamelody</groupId>
            <artifactId>javamelody-spring-boot-starter</artifactId>
            <version>1.92.0</version>
        </dependency>

And the application throws error at validationService as Null pointer This is my AuthenticationService class

@Slf4j
@Service
public class AuthenticationService implements IAuthenticationService {
    @Autowired
    private ValidationService validationService;
    @Autowired
    private UserLoginDao userLoginDao;

public TempLoginModel doSoftLogin(@Valid UserLoginRequest request) {

        UserLogin userLogin = userLoginDao.findByUsernameAndCorporateCode(request.getUsername())
                .orElseThrow(() -> new UsernameNotFoundException(
                        env.getProperty("message.authentication.service.wrong.credential")));

validationService.exceptionIfUserLoginNotActivated
                .andThen(validationService.exceptionIfUserLoginLocked)
.accept(userLogin);


        String jwtToken = JWTUtil.createJwt(request.getUsername(),
                userLogin.getUserInformation().getMobileNumber());

        return new TempLoginModel(jwtToken);

    }
}

My validationService class is as

@Slf4j
@Service
public class ValidationService {
    public Consumer<UserLogin> exceptionIfUserLoginNotActivated = (user) -> {
        if (!user.isActivated()) {
            throw new NoAccessException(env.getProperty("message.user.login.not.activate"));
        }
    };

    public Consumer<UserLogin> exceptionIfUserLoginLocked = (user) -> {

        if (user.getWrongLoginAttempt() >= getWrongPasswordAttemptCount()) {
            throw new NoAccessException(env.getProperty("message.user.login.locked"));
        }

    };

}

I have a controller which directly calls the authenticationService

The exception is thrown as

java.lang.NullPointerException: null
    at com.java.corporate.service.impl.AuthenticationService.exceptionIfInvalidLogin(AuthenticationService.java:355)
    at com.java.corporate.service.impl.AuthenticationService.doSoftLogin(AuthenticationService.java:190)
    at com.java.corporate.service.impl.AuthenticationService$$FastClassBySpringCGLIB$$1d9d9fd9.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
    at net.bull.javamelody.MonitoringSpringInterceptor.invoke(MonitoringSpringInterceptor.java:76)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)

What additional configurations should be done for the java-melody monitoring to function properly?

My main method is

public TempLoginModel doSoftLogin(@Valid UserLoginRequest request) {

        UserLogin userLogin = userLoginDao.findByUsername(request.getUsername())
                .orElseThrow(() -> new UsernameNotFoundException(
                        env.getProperty("message.authentication.service.wrong.credential")));

        exceptionIfInvalidLogin(request.getPassword(), userLogin);
}


Sudan Shrestha
  • 97
  • 1
  • 10
  • Please provide a [mre]. – kiner_shah Mar 09 '23 at 06:25
  • All the autowired services are null. I have only added the dependency of java-melody in my application. It works fine if i remove the dependency but throws error when i add the melody dependency in pom file. – Sudan Shrestha Mar 09 '23 at 06:37
  • The method is `private` so you are doing an internal method call on a proxy. A proxy doesn't have any dependencies associated with it. HOwever I do expect that the real culprit is your `doSoftLogin` method which you probably made `final` like your other mehods. `final` methods (and classes) cannot be proxied and will lead to this behavior. – M. Deinum Mar 09 '23 at 06:38
  • No my doSoftLogin method is not final. I also just removed final from my other methods but still the same issue persists – Sudan Shrestha Mar 09 '23 at 06:50
  • I have added doSoftLogin method also – Sudan Shrestha Mar 09 '23 at 06:52
  • My spring boot version is 2.2.9.RELEASE – Sudan Shrestha Mar 09 '23 at 07:16
  • as stated it is an internal method call on a `private` method, which will be, eventually, invokled on the proxy and not the underlying object. Also the `doSoftLogin` isn't the main method that is `invoke`. Please add your full class instead of snippets. – M. Deinum Mar 09 '23 at 08:14
  • I have updated the question. doSoftLogin is the main method as it is called directly from the controller – Sudan Shrestha Mar 09 '23 at 08:48

0 Answers0