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.
//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(); }
Thanks you for reading this issue.
I hope these solutions may be worth to you.
Thanks for reporting. Will fix this for the next release
Fixed, avoiding NPE due to missing executor. Thanks for reporting!
:)