# Painless null pointer exception

**URL:** https://discuss.elastic.co/t/painless-null-pointer-exception/128245
**Category:** Elasticsearch
**Created:** [April 16, 2018, 7:35pm UTC](https://discuss.elastic.co/t/painless-null-pointer-exception/128245 "2018-04-16T19:35:08Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Syed\_ali\_abbas\_shah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/syed_ali_abbas_shah/32/30053_2.png) [@Syed\_ali\_abbas\_shah](https://discuss.elastic.co/u/Syed_ali_abbas_shah)
#### Post date: [April 16, 2018, 7:35pm UTC](https://discuss.elastic.co/t/painless-null-pointer-exception/128245/1 "2018-04-16T19:35:08Z")

</div>

Here is My query , I am using elastic search 6.2 and my java version is 1.9

```auto
GET /users_small/user/_search
{
  "_source": [
    "id",
    "username",
    "full_name",
    "picture",
    "message_privacy"
  ],
  "from": 0,
  "size": 1000,
  "sort": [
    {
      "username.raw": {
        "order": "asc"
      }
    }
  ],
  "query": {
    "bool": {
      "must": [
         {
          "terms": {
            "id": [
              5
            ]
          }
        },
        {
          "terms": {
            "message_privacy": [
              0,
              1
            ]
          }
        },
         {
          "terms": {
            "id": [
              5
            ]
          }
        },
        {
          "script": {
            "script": {
              "source": "List followings = new ArrayList(params._source['followings']); List followers = new ArrayList(params._source['followers']); if(followers.contains(params.keyword)) { return true } else if(followings.contains(params.keyword)) { return true; } else { return false; }",
              "lang": "painless",
              "params": {
                "keyword": 14
              }
            }
          }
        }
      ],
      "must_not": [
        {
          "terms": {
            "id": [
              "7195",
              "1154",
              "3464",
              "8562",
              "2874",
              "6522",
              "8721",
              "9196",
              "3318",
              "13986",
              "8693",
              "7201",
              "9546",
              "7240",
              "18042",
              "6013",
              "6999",
              "19419",
              "3984",
              "17688",
              "9244",
              "28335",
              "850",
              "28777",
              "8625",
              8625
            ]
          }
        }
      ],
      "filter": []
    }
  }
}

```

but i am getting following error

```auto
{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 4,
    "skipped": 0,
    "failed": 1,
    "failures": [
      {
        "shard": 1,
        "index": "users_small",
        "node": "mK14IuVHTDKgHLnYYoXGqQ",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "followings = new ArrayList(params._source['followings']); List ",
            " ^---- HERE"
          ],
          "script": "List followings = new ArrayList(params._source['followings']); List followers = new ArrayList(params._source['followers']); if(followers.contains(params.keyword)) { return true } else if(followings.contains(params.keyword)) { return true; } else { return false; }",
          "lang": "painless",
          "caused_by": {
            "type": "null_pointer_exception",
            "reason": null
          }
        }
      }
    ]
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

```

---

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [April 17, 2018, 4:35am UTC](https://discuss.elastic.co/t/painless-null-pointer-exception/128245/2 "2018-04-17T04:35:20Z")

</div>

Are you sure that all your documents contain a non-null `followings` array?

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [April 17, 2018, 1:00pm UTC](https://discuss.elastic.co/t/painless-null-pointer-exception/128245/3 "2018-04-17T13:00:46Z")

</div>

Unfortunately, `_source` is not accessible in script queries, you would have to use doc values for that. One thing I don't quite understand from your example is why use script here. You would get much better performance if you replace it with 2 term queries.

---

<div class="post-metadata">

### Author: ![Syed\_ali\_abbas\_shah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/syed_ali_abbas_shah/32/30053_2.png) [@Syed\_ali\_abbas\_shah](https://discuss.elastic.co/u/Syed_ali_abbas_shah)
#### Post date: [April 19, 2018, 7:07pm UTC](https://discuss.elastic.co/t/painless-null-pointer-exception/128245/4 "2018-04-19T19:07:02Z")

</div>

Yes atleast every document has empty node like "following" : []

---

<div class="post-metadata">

### Author: ![Syed\_ali\_abbas\_shah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/syed_ali_abbas_shah/32/30053_2.png) [@Syed\_ali\_abbas\_shah](https://discuss.elastic.co/u/Syed_ali_abbas_shah)
#### Post date: [April 19, 2018, 7:07pm UTC](https://discuss.elastic.co/t/painless-null-pointer-exception/128245/5 "2018-04-19T19:07:39Z")

</div>

doc value is also not working , it gives us to allow metadata = true which can cause memory issues

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [April 20, 2018, 5:47am UTC](https://discuss.elastic.co/t/painless-null-pointer-exception/128245/6 "2018-04-20T05:47:20Z")

</div>

> [@Syed\_ali\_abbas\_shah](#):
>
> it gives us to allow metadata

You should use a not-analyzed version of this field for that. You might have `followings.keywords` for example if you used default settings and is using a recent version of elasticsearch.

---

<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: [May 18, 2018, 5:47am UTC](https://discuss.elastic.co/t/painless-null-pointer-exception/128245/7 "2018-05-18T05:47:20Z")

</div>

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