NullPointerException in HibernateEventInterceptor

I’m using OpenMRS 1.11.4 and Reference Application 2.3.1 (with bundled modules) in my development environment.

I’m developing a custom module containing some custom API code (Entities, Services and DAO implementation).

Now, I’ve written REST resources for the entities created above. Let us consider one of the entities InformationRequest and I’ve implemented InformationRequestResource1_11 as you can find in the following attachment:

Now when I try to create InformationRequest by POST invocation of the above REST implementaiton, I get the following exception in the logs:

Does it have anything to do with? Current user: Guest (Not logged in) Are you able to reproduce it if you do not run in a development or debug environment?

@dkayiwa, I get the same error “Current user: Guest (Not logged in)” even if I use the application by logging-in as Super User or some other user with Organizational roles.

I’m not sure what you’re trying to say here.

Are you running this from an IDE? Or in debug mode?

Testing the REST implementation using Postman.

Also tried it using jQuery AJAX from one of the reference application pages but still getting the same exception. The only thing that changes in the error log is “Current user: Username of logged-in User” instead of “Current user: Guest (Not logged in)”.

Is the server running from IDE? Or is it running under jetty in debug mode? Or something else?

@dkayiwa, the application is deployed in tomcat container with normal settings.

Just for troubleshooting purposes, are you able to reproduce this with the reference application version 2.5?

Sadly I won’t be able to do that @dkayiwa as I’ve some important custom logic sitting in my referenceapplication module without which I won’t be able to run the application.

@themoonraker13, taking a quick peek at the code is not very helpful: https://github.com/openmrs/openmrs-module-event/blob/2.2.1/api/src/main/java/org/openmrs/event/api/db/hibernate/HibernateEventInterceptor.java#L72

Can you debug this, and see where the NPE is coming from?

On running the application with debug log level, I found this: ... INFO - LoggingAdvice.invoke(115) |2017-01-17 18:22:05,227| In method InformationSharingService.saveInformationRequest. Arguments: InformationRequest=InformationRequest[hashCode=2fe57f6e,uuid=6a7dcb72-2edd-4632-ade9-dbd4ef1f9eb7], DEBUG - AuditableInterceptor.setCreatorAndDateCreatedIfNull(107) |2017-01-17 18:22:05,232| Setting creator and dateCreated on InformationRequest[hashCode=2fe57f6e,uuid=6a7dcb72-2edd-4632-ade9-dbd4ef1f9eb7] ERROR - LoggingAdvice.invoke(135) |2017-01-17 18:22:05,234| An error occurred while executing this method. Current user: doctor@demo.com Error message: null java.lang.NullPointerException at org.openmrs.event.api.db.hibernate.HibernateEventInterceptor.onSave(HibernateEventInterceptor.java:72) ...

Does the exception have anything to do with inserting audit info??

Which version of the module are you running? I highly suspect inserts is what is null on line 72 above, which only happens if there is no transaction that was started which occurs when you don’t have transactions configured properly. You might want to share your code where you setup transactions to see if they are configured well.

@wyclif, do you mean to say the spring and hibernate configurations??

Also, as you said there seems to be a transaction related issue. The REST response for the POST request is a JSON of the resource object but I don’t find any record created in database.

Is the resource you are trying to get in core or a module? If it’s in a module you might confirm the transaction configurations on the service methods to ensure that they are correct. If it’s in core what domain object does your resource return.

It’s a module @wyclif and the domain object is also a custom entity.

I’ve 1 service class holding all the methods which in tern invoke the HibernateDAO methods. What should be standard transaction configurations for the service??

Are you suggesting that I should mark service methods as @Transactional??

Can you share your code please?

That won’t be needed anymore @wyclif :slight_smile:

Adding @Transactional on service methods solved the problem for me.

Thanks a lot @wyclif, @dkayiwa and @darius.

2 Likes