# Painless create new object

**URL:** <https://discuss.elastic.co/t/painless-create-new-object/254059>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [November 2, 2020, 5:07pm UTC](https://discuss.elastic.co/t/painless-create-new-object/254059 "2020-11-02T17:07:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![christophe\_spielmann](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christophe_spielmann/32/49567_2.png) [@christophe\_spielmann](https://discuss.elastic.co/u/christophe_spielmann)\
**Post date:** [November 2, 2020, 5:07pm UTC](https://discuss.elastic.co/t/painless-create-new-object/254059/1 "2020-11-02T17:07:06Z")

</div>

I have a rather basic requirement, I would like to add an object to a document with painless within a query for update.

This is the object I would like to add

```auto
'"memoire": {
      "codeActe": "C",
      "docType": "M",
      "uuid": "added ",
      "nonFacturable": false,
      "published": false
    }, 

```

I tried

```auto
    POST /csa_encounters/_update_by_query
{
  "script":
{ "source": """

  ctx._source.memoire.codeActe = "C";
  ctx._source.memoire.docType= "M";
  ctx._source.memoire.uuid = "added ";
  ctx._source.memoire.nonFacturable = false;
  ctx._source.memoire.published =false
""",
"lang": "painless"
},
    "query": {
        "bool" : {
            "must" : [
                { "query_string" : { "query" : "140c7646", "fields" : ["_id"] } }
            ]
        }
    }
}    

```

But I get the message null pointer exception as the field does not exist of course

```auto
"type": "script_exception",
    "reason": "runtime error",
    "script_stack": [
      "ctx._source.memoire.codeActe = \"C\";\n ",
      " ^---- HERE"
    ],
 ...
    "lang": "painless",
    "caused_by": {
      "type": "null_pointer_exception",
      "reason": null
    }
  },
  "status": 400

```

I am not able to figure out how to create the object

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 30, 2020, 5:07pm UTC](https://discuss.elastic.co/t/painless-create-new-object/254059/2 "2020-11-30T17:07:31Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
