0

I'm trying to inject a class called MeasurementService into an AspectJ Annotation class called MeasurementAspect but I received an error of NullPointerException and I checked that the injection of MeasurementService is null in this case. The injected class itself is then triggered in one of the advices.

The MeasurementAspect class itself doesn't have constructor as I think there is no need to have it and all advice will be injected to the appropriate methods and executed automatically.

Here is the excerpt of MeasurementAspect (as it is too long to copy all):

    @Aspect
    public class MeasurementAspect {
...   
    private @Inject MeasurementService measurementService;
    private final MeasureDownloadDto measureDownloadDto = new MeasureDownloadDto();
    private final MeasureUploadDto measureUploadDto = new MeasureUploadDto();

...

    @Around("execution(* *.storeFile(..))")
        public Object CalculateUploadMetadata(ProceedingJoinPoint joinPoint) throws Throwable {
            start = System.nanoTime();
            Object result = joinPoint.proceed();
            end = System.nanoTime();
            duration = end - start;
            setDurationUploadMetadata(duration);
            measureUploadDto.setDurationUploadMetadata(duration);
            System.err.println(
                    SDF.format(new Date()) + " Elapsed time for uploading meta data (ns) = " + getDurationUploadMetadata());
            System.err.println("measurementService" + measurementService);
            measurementService.storeUploadDto(measureUploadDto);
            durUploadEachChunk.clear();
            return result;
        }
...
}

Here is the MeasurementService class that will be injected:

public class MeasurementService {

    private final RemoteStatisticService remoteStatisticService;

    @Inject
    public MeasurementService(RemoteStatisticService remoteStatisticService) {
        this.remoteStatisticService = remoteStatisticService;
    }

    public void storeUploadDto(MeasureUploadDto measureUploadDto) {
        remoteStatisticService.postUploadStatistic(measureUploadDto);
    }

    public void storeDownloadDto(MeasureDownloadDto measureDownloadDto) {
        remoteStatisticService.postDownloadStatistic(measureDownloadDto);
    }

}

Any help is appreciated. Thanks

ps: It is still related to my another question NoAspectBoundException error in class constructor of AspectJ class with Dependency Injection , I hope it can give whole point of view of the problem

Community
  • 1
  • 1
Ihsan Haikal
  • 1,085
  • 4
  • 16
  • 42
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Idos May 19 '16 at 09:48
  • Maybe you've got a point there but still I couldn't figure out how to solve my problem @Idos – Ihsan Haikal May 19 '16 at 09:54
  • I gave an answer to your problem in your linked question. That should solve your problem. http://stackoverflow.com/a/37345027/2699901 – Nándor Előd Fekete May 26 '16 at 17:53
  • @NándorElődFekete I already replied to your answer, could you please check it? Thanks – Ihsan Haikal May 27 '16 at 08:21

0 Answers0