I am using Facade pattern to access by database entities. I have written a wrapper to access the Facade EJB like below. As I understand from the exception, it seems like the EJB is notyet initialized. After reading about the exception on forums, I understood that it should be resolved @PostConstruct notation but still no help. May be I using it wrong, any pointers will be greatly appreciated
public class PatientSearchHelper {
@EJB
private PatientFacade patientFacade;
private final Patient patient;
private ResponseHeader respHeader;
private SearchResponse searchResponse;
private List<Patient> resultSet;
public PatientSearchHelper (Patient patient) {
this.patient = patient;
}
@PostConstruct
public void initialize() {
this.respHeader = new ResponseHeader();
this.searchResponse = new SearchResponse();
}
public SearchResponse getById() {
System.out.println("Patient Id: " + patient.getPatientid());
//patientFacade = (PatientFacade) new InitialContext().lookup("java:global/Aarogayam2/PatientFacade!common.facades.PatientFacade");
resultSet = patientFacade.findById(patient.getPatientid());
if (resultSet.size() > 0) {
formatFoundResponse();
} else {
formatNotFoundResponse();
}
return searchResponse;
}
private void formatFoundResponse() {
searchResponse.setPayload(resultSet);
respHeader.setSuccess(true);
searchResponse.setHeader(respHeader);
}
private void formatNotFoundResponse() {
respHeader.setSuccess(false);
respHeader.setMessage("No Patient found");
searchResponse.setHeader(respHeader);
searchResponse.setPayload(null);
}
}
However I get the exception when calling it like below
PatientSearchHelper searchHelper = new PatientSearchHelper(patient);
searchHelper.initialize();
return searchHelper.getById();
Exception
SEVERE: java.lang.NullPointerException
at common.helpers.PatientSearchHelper.getById(PatientSearchHelper.java:48)
at common.services.PatientService.getById(PatientService.java:57)
at common.services.PatientService$Proxy$_$$_WeldSubclass.getById(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
If you want to make container to create EJB instances, you need to access using using JNDI or @EJB annotation. In order to make the above code to work make PatientSearchHelper class an EJB and use @EJB in your client code to get the instance before accessing any methods.