Menu

#223 NullPointerException using DefaultHapiContext .close method

2.3
closed-fixed
None
5
2014-12-16
2014-12-12
No

Using the class DefaultHapiContext, I got a NullPointerException at ca.uhn.hl7v2.DefaultHapiContext.close(DefaultHapiContext.java:159).

I just create a new instance of DefaultHapiContext with empty constructor to use the parsers.

//My program
HapiContext context = new DefaultHapiContext();

Parser p = context.getGenericParser();

/*some code more*/

try {
    context.close();
} catch (IOException e1) {      
    e1.printStackTrace();
}  

At the end I use method close to close the context instance. Eclipse warning recommends close the context. I get a null pointer exception when I call context.close();

Looking into .close source code.

//ca.uhn.hl7v2.DefaultHapiContext
public void close() throws IOException {
    getConnectionHub().discardAll();
    if (DefaultExecutorService.isDefaultService(executorService)) {
        executorService.shutdownNow();
    }
}

I get the NullPointer because this is calling executorService.shutdownNow() when
executorService is null. If sentence returns true because

//ca.uhn.hl7v2.concurrent.DefaultExecutorService
public static boolean isDefaultService(ExecutorService service) {
    return service == defaultExecutorService;
}

executor services is null and defaultExecutorService is also null.
isDefaultService(ExecutorService service) do the comparison, null == null.

ExecutorService defaultExecutorService inside class DefaultExecutorService is non initialized.
It is null.

ca.uhn.hl7v2.concurrent.DefaultExecutorService just initialized the var
ExecutorService defaultExecutorService
when you call DefaultExecutorService.getDefaultService() method.

DefaultHapiContext empty constructor not call DefaultExecutorService.getDefaultService() neither close method check if var executorService is null.


A workaround to fix it is after call


HapiContext context = new DefaultHapiContext();
call
context.getExecutorService();
because DefaultHapiContext has a method getExecutorService that

//ca.uhn.hl7v2.DefaultHapiContext
public synchronized ExecutorService getExecutorService() {
    if (executorService == null) {
        executorService = DefaultExecutorService.getDefaultService();
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                executorService.shutdownNow();
            }

        });
    }
    return executorService;
}

initialize var executorService when it's null.

//My program
HapiContext context = new DefaultHapiContext();
context.getExecutorService();

Parser p = context.getGenericParser();

/*some code more*/

try {
    context.close();
} catch (IOException e1) {      
    e1.printStackTrace();
}  



To fix it:


DefaultHapiContext should initialize its var executorService in its constructor or
check in close method if executorService is null and return false.

Thanks you for reading this issue.
I hope these solutions may be worth to you.

Discussion

  • Christian Ohr

    Christian Ohr - 2014-12-15

    Thanks for reporting. Will fix this for the next release

     
  • Christian Ohr

    Christian Ohr - 2014-12-15
    • Group: 2.2 --> 2.3
     
  • Christian Ohr

    Christian Ohr - 2014-12-16
    • assigned_to: Christian Ohr
     
  • Christian Ohr

    Christian Ohr - 2014-12-16
    • status: open --> closed-fixed
     
  • Christian Ohr

    Christian Ohr - 2014-12-16

    Fixed, avoiding NPE due to missing executor. Thanks for reporting!

     
  • Domingo Amado

    Domingo Amado - 2014-12-16

    :)

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.