# How to catch a null\_pointer\_exception?

**URL:** https://discuss.elastic.co/t/how-to-catch-a-null-pointer-exception/353921
**Category:** Kibana
**Tags:** painless
**Created:** [February 22, 2024, 8:25pm UTC](https://discuss.elastic.co/t/how-to-catch-a-null-pointer-exception/353921 "2024-02-22T20:25:37Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![catalin8](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/catalin8/32/132014_2.png) [@catalin8](https://discuss.elastic.co/u/catalin8)
#### Post date: [February 22, 2024, 8:25pm UTC](https://discuss.elastic.co/t/how-to-catch-a-null-pointer-exception/353921/1 "2024-02-22T20:25:37Z")

</div>

On a reindex operation I'm using

```auto
if(ctx._source.containsKey("author")) {
    ctx._source.title = ctx._source.title + ctx._source.author[0];
    ctx._source.remove("author");
}

```

Which throws a null pointer exception error:

```auto
"lang":"painless","caused_by":{"type":"null_pointer_exception","reason":null}},"status":400}

```

How can I check which document causes this error? Because I made sure the `author` field exists, is an array, and the 0 index exists, so I have no idea why I still get the null pointer exception

---

<div class="post-metadata">

### Author: ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)
#### Post date: [February 24, 2024, 1:06am UTC](https://discuss.elastic.co/t/how-to-catch-a-null-pointer-exception/353921/2 "2024-02-24T01:06:02Z")

</div>

What versions are you working with? You might have documents without the `_source` field. Easiest way to check and fix if that's the case is to wrap it all in another `IF` clause that checks for the `_source` field before everything.

---

<div class="post-metadata">

### Author: ![catalin8](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/catalin8/32/132014_2.png) [@catalin8](https://discuss.elastic.co/u/catalin8)
#### Post date: [February 25, 2024, 5:44pm UTC](https://discuss.elastic.co/t/how-to-catch-a-null-pointer-exception/353921/3 "2024-02-25T17:44:59Z")

</div>

I'm using Elasticsearch 7.6 and `ctx._source` always works. But for some reason not when performing a reindex operation.

What I did as a result was to use a query to check if the field exists in the source index.

---

<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: [March 24, 2024, 5:45pm UTC](https://discuss.elastic.co/t/how-to-catch-a-null-pointer-exception/353921/4 "2024-03-24T17:45:47Z")

</div>

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