Error: Variable serverBase in RadiologyHandler is returning null

I claimed the issue FM-172 which requires tests to be added for RadiologyHandler. The test for getFHIRDiagnosticReportBySubjectName() in RadiologyHandler is throwing the following error:

java.lang.NullPointerException
        at java.lang.StringBuilder.<init>(StringBuilder.java:112)
        at ca.uhn.fhir.rest.client.apache.ApacheRestfulClientFactory.getHttpClient(ApacheRestfulClientFactory.java:72)
        at ca.uhn.fhir.rest.client.apache.ApacheRestfulClientFactory.getHttpClient(ApacheRestfulClientFactory.java:48)
        at ca.uhn.fhir.rest.client.RestfulClientFactory.newGenericClient(RestfulClientFactory.java:190)
        at ca.uhn.fhir.context.FhirContext.newRestfulGenericClient(FhirContext.java:554)
        at org.openmrs.module.fhir.api.util.FHIRRESTfulGenericClient.searchWhereReferenceAndToken(FHIRRESTfulGenericClient.java:62)
        at org.openmrs.module.fhir.api.diagnosticreport.handler.RadiologyHandler.getFHIRDiagnosticReportBySubjectName(RadiologyHandler.java:78)
        at org.openmrs.module.fhir.api.diagnosticreport.handler.RadiologyHandlerTest.getFHIRDiagnosticReportBySubjectName_retrunListOfDiagnosticReports(RadiologyHandlerTest.java:199)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
        at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
        at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
        at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:365)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:273)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:159)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345)
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418)

On exploring, I found out that the variable serverBase in RadiologyHandler was returning null which was causing the exception. The result of FHIRUtils.getDiagnosticReportRadiologyBaseServerURL() is assigned to the serverBase variable and suspect the problem to be with the return of getDiagnosticReportRadiologyBaseServerURL() in FHIRUtils

Here is my PR- https://github.com/openmrs/openmrs-module-fhir/pull/203

Any suggestions on solving this? :slight_smile:

Sorry about the error kindly try using pastebin to share the error log. Kindly attach the link to your pull request on the ticket for easy review. https://wiki.openmrs.org/display/docs/Pull+Request+Tips

Thank you for the reply :slight_smile: . Here is the pastebin link- https://pastebin.com/j6B2yHBL .

@haripriya, am not exactly understanding the exact problem.

because am seeing a PR and the same time a you need a solution. What does your Pr do ?? and for what problem do you need a solution . please make for me some clarification

does the error you reported occur before or after you added your local changes??

is that an existing test already or one of the tests you added??

The issue was to create a test file for RadiologyHander.java . Test for getFHIRDiagnosticReportBySubjectName() is the one added by me. The error is being caused because the variable serverBase is being set to null as getDiagnosticReportRadiologyBaseServerURL() in FHIRUtils file is returning null value (Previously written code) which is causing the exception in the test function.

  @Test
    public void getFHIRDiagnosticReportBySubjectName_retrunListOfDiagnosticReports()
    {
    	RadiologyHandler radiologyHandler=new RadiologyHandler();
    	Patient patient=createPatient();
    	Encounter encounter = createEncounter(patient.getUuid());
    	String name="";
    	for(PersonName i:patient.getNames())
    	{
    	name=i.getGivenName();
    	break;
    	}
    	try {
       assertNotNull(radiologyHandler.getFHIRDiagnosticReportBySubjectName(name));
    }
    	catch(Exception e)
    	{
    		e.printStackTrace();
    	}
    }

is this the test your talking about??

Yes,This is the test i was talking about :slight_smile:

Now @haripriya, since we don’t not include any dummy data when writing classes, thats the right expected behaviour . What i mean , is whenever you write a test , ensure all associated variables to whichever method your writing a test for , have been assigned some values. So you just have to find a way of assigning some value to the variable serverBase for your testing-use-case otherwise , it would be expected to throw a nullPointerException

That is exactly what i realized while writing the test. I am unsure of what the variable serverBase is supposed to be set to.

@haripriya whenever u call a method on a variable that has been declared as null it automatically throws a nullPointerException Thanks @mozzy for your guidance on this

1 Like

@haripriya here is where that string is set from

String serverBase = FHIRUtils.getDiagnosticReportRadiologyBaseServerURL();

and this is the util method

    public static String getDiagnosticReportRadiologyBaseServerURL() {
    		return Context.getAdministrationService().getGlobalProperty("fhir.diagnosticreport.radiology.server");
    	}

in other words that string is kind of a global property value

Now in your tests you can try setting a Global property value just for your test case use …

some thing like

Context.getAdministrationService().SetGlobalPropertyValue("fhir.diagnosticreport.radiology.server");

thats is just a crue , try to look at the suitable GlobalProperty method , and see which one can work best for you. in otherwords , when you asign a value to that GP in your tests , the variable servebaseurl will now be assigned a value…

What value should I set the GP “fhir.diagnosticreport.radiology.server” to?

Not sure , but run your local installation of openmrs and take a look at the default value that is set to it. it can give you a crue.

i tried doing this, but it did’nt really yield any result.

ok, now your going to create a dataset for that global property and just set the value in the dataset instead .

<dataset>
<global_property property="fhir.diagnosticreport.radiology.server"  property_value= "some value" description=""/>
<dataset>

then in your test , do execute(dataset-name)

1 Like

Can you please help me understand this error

java.lang.IllegalArgumentException: When using the client with HL7.org structures, you must specify the bundle return type for the client by adding ".returnBundle(org.hl7.fhir.instance.model.Bundle.class)" to your search method call before the ".execute()" method
        at ca.uhn.fhir.rest.client.GenericClient$SearchInternal.execute(GenericClient.java:2110)
        at ca.uhn.fhir.rest.client.GenericClient$SearchInternal.execute(GenericClient.java:1956)
        at org.openmrs.module.fhir.api.util.FHIRRESTfulGenericClient.searchWhereReferenceAndToken(FHIRRESTfulGenericClient.java:67)
        at org.openmrs.module.fhir.api.diagnosticreport.handler.RadiologyHandler.getFHIRDiagnosticReportBySubjectName(RadiologyHandler.java:77)
        at org.openmrs.module.fhir.api.diagnosticreport.handler.RadiologyHandlerTest.getFHIRDiagnosticReportBySubjectName_retrunListOfDiagnosticReports(RadiologyHandlerTest.java:203)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
        at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
        at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
        at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:365)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:273)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:159)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345)
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418)

This is what I’m getting when I set the global property

you must have made some mistake some where, passing an inapropriate or null parameter to this method.RadiologyHandlerTest.getFHIRDiagnosticReportBySubjectName_retrunListOfDiagnosticReports , that error is thrown when the method has been passed an illegal or inappropriate argument or null argument.

1 Like