"contextDAO is null" error doing API example with Eclipse

Tags: #<Tag:0x00007f0f278e31d0>

**Platform Version:**1.11.2. OpenMRS Version: OpenMRS 2.2.

Question: Hello everyone: I need your help, I try to do the example, but it doesn’t work https://wiki.openmrs.org/display/docs/How+To+Use+the+OpenMRS+API

i work with Eclipse Luna, and I create a new Class called “Test”

In this Class I pasted the next code:

but this created a problem with “Context.startup()”, reason i made the following:

But when I run the application delivers the following result:

I don’t know what’s happens, please help.

Hi @altair141, why do you pass a new Properties object to Context.startup(...) instead of using props. Not that this is the cause of the error, still working through that.

Hi @gwasilwa, because I was trying with other options of instantiate the class Properties, (I was thinking that this was the problem, but not)

also I tried with this:

… but the same error.

Hi @altair141 I did some more digging and found out that the Context class has some fields, namely contextDAO and serviceContext which are being managed by Spring. Spring has not been initialized in your case, hence the null contextDAO.

PS: You should try the REST approach, since this is no longer supported.

Hi, @gwasilwa, I have searched, but i have not found information to initialize Spring in openmrs. Do You know how to initialize openmrs? or do You know of any example published?

About rest, this is the second option, but the priority is OpenMrs-API

Thank you very much for your help. :smiley:

Hi,

Any one Resolve that problem please Share me. How to resolve Context is null Error

log4j:WARN No appenders could be found for logger (org.openmrs.api.context.Context). log4j:WARN Please initialize the log4j system properly. Exception in thread “main” org.openmrs.api.APIException: contextDAO is null at org.openmrs.api.context.Context.getContextDAO(Context.java:162) at org.openmrs.api.context.Context.openSession(Context.java:740) at org.openmrs.api.context.Context.startup(Context.java:903) at Test.main(Test.java:23)

Can you share the entire stack trace at pastebin.com?

This is my code

	  try {	String url= "jdbc:mysql://localhost:3306/openmrs?autoReconnect=true&sessionVariables=default_storage_engine=InnoDB&useUnicode=true&characterEncoding=UTF-8";
		 // Context.startup("jdbc:mysql://192.168.0.172/openmrs?autoReconnect=true", "uopenmrs", "openmrs", props);
	   Context.startup("jdbc:mysql://192.168.0.172/openmrs?autoReconnect=true&sessionVariables=default_storage_engine=InnoDB&useUnicode=true&characterEncoding=UTF-8", "uopenmrs", "openmrs", new Properties());  
		System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
	  } catch (ModuleMustStartException | DatabaseUpdateException | InputRequiredException e) {
		System.out.println("----------------------");
		e.printStackTrace();
	}
	  try {
	    Context.openSession();
	    System.out.println("govind start");
	    Context.authenticate("superman", "Admin123");
	    System.out.println("govind run");
	    List<Patient> patients = Context.getPatientService().getPatients("govind");
	    System.out.println("govind stop");
	    for (Patient patient : patients) {
	      System.out.println("Found patient with name " + patient.getPersonName() + " and uuid: " + patient.getUuid());
	    }
	    
	  }
	  finally {
	    Context.closeSession();
	  }
	}

I want connect openmrs data for fetching Person record via our own code .how this possible

I am Getting Error

log4j:WARN No appenders could be found for logger (org.openmrs.api.context.Context).log4j:WARN Please initialize the log4j system properly.Exception in thread "main" org.openmrs.api.APIException: contextDAO is null
at org.openmrs.api.context.Context.getContextDAO(Context.java:162)
at org.openmrs.api.context.Context.openSession(Context.java:740)
at org.openmrs.api.context.Context.startup(Context.java:903)
at Test.main(Test.java:23)