Java.lang.NullPointerException: Cannot get property 'x' on null object - when running a groovy script

Hi,

I run a groovy script through the transport client:
The script looks like this:

if (ctx._source.field.names == null) 
         ctx._source.field.names=newItems 
      else 
         ctx._source.field.names<< newItems; 
         ctx._source.field.names = ctx._source.field.names.flatten().unique()

My problem is that most of the times it works but sometimes I get the following error:

 Caused by: java.lang.IllegalArgumentException: failed to execute script 
   at org.elasticsearch.action.update.UpdateHelper.executeScript(UpdateHelper.java:256) ~[org.elasticsearch.elasticsearch-2.0.1.jar:2.0.1] 
   at org.elasticsearch.action.update.UpdateHelper.prepare(UpdateHelper.java:196) ~[org.elasticsearch.elasticsearch-2.0.1.jar:2.0.1] 
   at org.elasticsearch.action.update.UpdateHelper.prepare(UpdateHelper.java:79) ~[org.elasticsearch.elasticsearch-2.0.1.jar:2.0.1] 
   at org.elasticsearch.action.update.TransportUpdateAction.shardOperation(TransportUpdateAction.java:170) ~[org.elasticsearch.elasticsearch-2.0
   at org.elasticsearch.action.update.TransportUpdateAction$3$1.doRun(TransportUpdateAction.java:227) ~[org.elasticsearch.elasticsearch-2.0.1.
   at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) ~[org.elasticsearch.elasticsearch-2.0.1.jar:2.0.1]
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_74-cedar14] 
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_74-cedar14] 
   at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_74-cedar14] 
 Caused by: org.elasticsearch.script.groovy.GroovyScriptExecutionException: failed to run inline script [if (ctx._source.field.names == null) 
     ctx._source.field.names=newItems 
  else 
     ctx._source.field.names<< newItems; 
     ctx._source.field.names = ctx._source.field.names.flatten().unique()] using lang [groovy] 
    at org.elasticsearch.script.groovy.GroovyScriptEngineService$GroovyScript.run(GroovyScriptEngineService.java:253) ~[org.elasticsearch.
    at org.elasticsearch.action.update.UpdateHelper.executeScript(UpdateHelper.java:251) ~[org.elasticsearch.elasticsearch-2.0.1.jar:2.0.1] 
    ... 8 common frames omitted 
  Caused by: java.lang.NullPointerException: Cannot get property 'names' on null object 
    at org.codehaus.groovy.runtime.NullObject.getProperty(NullObject.java:60) ~[org.codehaus.groovy.groovy-all-2.4.0.jar:2.4.0] 
    at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:172) ~[org.codehaus.groovy.groovy-all-2.4.0.jar:2.4.0] 
    at org.codehaus.groovy.runtime.callsite.NullCallSite.getProperty(NullCallSite.java:47) ~[org.codehaus.groovy.groovy-all-2.4.0.jar:2.4.0] 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:296) ~[org.codehaus.groovy.groovy-all-2.4.0.
    at 889babff0826c95e16279a9fb1fd2eb97a63e642.run(889babff0826c95e16279a9fb1fd2eb97a63e642:1) ~[na:na] 
    at org.elasticsearch.script.groovy.GroovyScriptEngineService$GroovyScript.run(GroovyScriptEngineService.java:248) ~[org.elasticsearch.

I looked on the document the script fails on, and tried to manually run this script using curl, and it worked.
So the document is not really the problem.

I am running this update for 38 documents, meaning it will run 38 times.
The exception is thrown on a different document every time I try it this update from the beginning (meaning running it again on those 38 documents)

Not sure what I am missing here. any ideas?
Thanks!