0

I have a problem with the injection in Java as I would like to inject an interface called RemoteStatisticService but it keeps returning null in this case thus error NullPointerException. I have tried to follow this with init() method and @PostConstruct but still gives me the same error.

Here is the code for MeasurementAspectService class:

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import *.dto.MeasureDownloadDto;
import *.dto.MeasureUploadDto;
import *.rs.RemoteStatisticService;

public class MeasurementAspectService {

    private @Inject RemoteStatisticService remoteStatisticService;

    public void storeUploadDto(MeasureUploadDto measureUploadDto) {

        remoteStatisticService.postUploadStatistic(measureUploadDto);

    }

    public void storeDownloadDto(MeasureDownloadDto measureDownloadDto) {

        remoteStatisticService.postDownloadStatistic(measureDownloadDto);

    }

    @PostConstruct
    public void init() {

    }

}

Here is the code for interface class RemoteStatisticService

import static *.util.RemoteServiceUtil.PRIV;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import *.dto.MeasureDownloadDto;
import *.dto.MeasureUploadDto;

@Path(PRIV + "stats")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public interface RemoteStatisticService {
    @POST
    @Path("upload")
    void postUploadStatistic(MeasureUploadDto mud);

    @POST
    @Path("download")
    void postDownloadStatistic(MeasureDownloadDto mdd);

}

Any help is appreciated. Thanks

Community
  • 1
  • 1
Ihsan Haikal
  • 1,085
  • 4
  • 16
  • 42

2 Answers2

1

The problem is that you've defined an aspect using aspectj but are trying to get a reference to a CDI bean. This isn't going to work.

This line here is the culprit:

private final MeasurementAspectService measurementAspectService = new MeasurementAspectService();

You'll need to use CDI to get a reference. If you're using CDI 1.1, you can use this snippet.

private final MeasurementAspectService measurementAspectService = CDI.current().select(MeasurementAspectService.class).get();

This is because AspectJ isn't intended for CDI use. Note that you can use interceptors in CDI as well.

John Ament
  • 11,595
  • 1
  • 36
  • 45
  • there is an error of CDI as it couldn't get reference to it. Do I need another package to be imported? I am using Maven and I believe i'm using Javax package imported from javax.validation group (artifact validation-api) of version 1.1.0.Final – Ihsan Haikal May 23 '16 at 12:00
  • ok I fixed the previous error, now the new error comes as "Exception while initializing MeasurementAspect: java.lang.IllegalStateException: Unable to locate CDIProvider" – Ihsan Haikal May 23 '16 at 12:57
  • Are you sure that you're using CDI? – John Ament May 25 '16 at 00:15
  • I'm not sure as well as I am using Maven and last I check I only use @Inject for this class – Ihsan Haikal May 26 '16 at 09:17
  • Well, what container are you deploying to? If tomcat, are you leveraging something like weld servlet for CDI? – John Ament May 26 '16 at 10:51
0

CDI 1.1+ works with implicit beans by default. You need to add a bean-defining annotation like @Dependent or @ApplicationScoped to any class you want to get picked up by CDI.

Harald Wellmann
  • 12,615
  • 4
  • 41
  • 63