# NullPointerException on script for-loop over nested values

**URL:** <https://discuss.elastic.co/t/nullpointerexception-on-script-for-loop-over-nested-values/20921>\
**Category:** Elasticsearch\
**Created:** [November 24, 2014, 10:35pm UTC](https://discuss.elastic.co/t/nullpointerexception-on-script-for-loop-over-nested-values/20921 "2014-11-24T22:35:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bjorn\_Zeutzheim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bjorn_zeutzheim/32/1083_2.png) [@Bjorn\_Zeutzheim](https://discuss.elastic.co/u/Bjorn_Zeutzheim)\
**Post date:** [November 24, 2014, 10:35pm UTC](https://discuss.elastic.co/t/nullpointerexception-on-script-for-loop-over-nested-values/20921/1 "2014-11-24T22:35:55Z")

</div>

I'm getting weird NullPointerExceptions as soon as I try to use a script  
with a for-loop over nested values. Through some tests I am sure that the  
variable "doc[...].values" I'm retrieving is of type "Longs". Adding any  
kind of null-checks did not fix this problem so I suppose it's a bug in  
elasticsearch...

Mapping:  
{  
"ppint" : {  
"mappings" : {  
"version" : {  
"\_meta" : {  
"model" : "...."  
},  
"properties" : {  
"classes" : { "type" : "string" },  
"codeTags" : {  
"type" : "nested",  
"properties" : {  
"count" : { "type" : "integer" },  
"name" : { "type" : "string" }  
}  
},  
"languages" : { "type" : "string" },  
"name" : { "type" : "string" },  
"namespaces" : { "type" : "string" },  
"package" : {  
"properties" : {  
"description" : { "type" : "string" },  
"id" : { "type" : "string" },  
"name" : { "type" : "string" }  
}  
}  
}  
}  
}  
}  
}

Query:  
{  
"query": {  
"function\_score": {  
"score\_mode": "first",  
"functions": [  
{  
"script\_score": {  
"script": "for (tag : doc['codeTags.count'].values)  
{ \_score + tag; }"  
}  
}  
]  
}  
}  
}

Error:  
{  
[aF4-pJ4-RuuqrYV9SCHmnQ][ppint][4]: QueryPhaseExecutionException [  
[ppint][4]: query [  
filtered (  
function score (  
ConstantScore(_:_),  
functions: [  
{  
filter(_:_),  
function [  
script [  
for (tag : doc['codeTags.count'].values)  
{ \_score + tag; }  
],  
params [  
null  
]  
]  
}  
]  
)  
)-\>cache(\_type:version)  
],  
from[0],  
size[10]: Query Failed [Failed to execute main query]  
];  
nested: NullPointerException;  
}

I hope you can help me with that problem.  
I really need the score to be dependent based on tag-count , or I would  
have to move to some other system than elasticsearch.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/a37b8116-12e5-480c-b3e4-1c36a84a36c8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a37b8116-12e5-480c-b3e4-1c36a84a36c8%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Bjorn\_Zeutzheim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bjorn_zeutzheim/32/1083_2.png) [@Bjorn\_Zeutzheim](https://discuss.elastic.co/u/Bjorn_Zeutzheim)\
**Post date:** [November 25, 2014, 12:35am UTC](https://discuss.elastic.co/t/nullpointerexception-on-script-for-loop-over-nested-values/20921/2 "2014-11-25T00:35:52Z")

</div>

Turns out a simple "\_score;" was missing at the end as a return value.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/01b39906-3aac-4338-90fc-87eab70b7db8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/01b39906-3aac-4338-90fc-87eab70b7db8%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 6, 2017, 12:47am UTC](https://discuss.elastic.co/t/nullpointerexception-on-script-for-loop-over-nested-values/20921/3 "2017-07-06T00:47:56Z")

</div>


