# MultiSearchRequest{Builder} throws NullPointerException in elasticsearch 1.3.2

**URL:** https://discuss.elastic.co/t/multisearchrequest-builder-throws-nullpointerexception-in-elasticsearch-1-3-2/21243
**Category:** Elasticsearch
**Created:** [December 13, 2014, 5:05pm UTC](https://discuss.elastic.co/t/multisearchrequest-builder-throws-nullpointerexception-in-elasticsearch-1-3-2/21243 "2014-12-13T17:05:17Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Veerapuram\_Varadhan](https://avatars.discourse-cdn.com/v4/letter/v/45deac/32.png) [@Veerapuram\_Varadhan](https://discuss.elastic.co/u/Veerapuram_Varadhan)
#### Post date: [December 13, 2014, 5:05pm UTC](https://discuss.elastic.co/t/multisearchrequest-builder-throws-nullpointerexception-in-elasticsearch-1-3-2/21243/1 "2014-12-13T17:05:17Z")

</div>

Hi,

If this is already answered, please point me to the thread. TIA for that.

Now, the problem:

I am having the following code to test MultiSearchRequest{Buuilder} and it  
always throws me NullPointerException (even if I just use matchAllQuery()  
in the SearchRequests added to it).

public void performMultiSearchQuery () {  
Client client = new TransportClient().addTransportAddress(new  
InetSocketTransportAddress("localhost", 9300));  
SearchRequestBuilder srb1 = client.prepareSearch()  
.setQuery(QueryBuilders.queryString("sev:1")).setSize(Integer.MAX\_VALUE);  
SearchRequestBuilder srb2 = client.prepareSearch()  
.setQuery(QueryBuilders.queryString("priority:50")).setSize(Integer.MAX\_VALUE);

MultiSearchResponse sr = client.prepareMultiSearch()  
.add(srb1.request())  
.add(srb2.request())  
.execute().actionGet();

// You will get all individual responses from  
MultiSearchResponse#getResponses()  
long nbHits = 0;  
for (MultiSearchResponse.Item item : sr.getResponses()) {  
SearchResponse response = item.getResponse();  
nbHits += response.getHits().getTotalHits();  
}  
logger.info("Total Number of Hits: " + nbHits);  
client.close();  
}

And the exception it throws is:

Exception in thread "main"  
org.elasticsearch.common.util.concurrent.UncategorizedExecutionException:  
Failed execution  
at  
org.elasticsearch.action.support.AdapterActionFuture.rethrowExecutionException(AdapterActionFuture.java:90)  
at  
org.elasticsearch.action.support.AdapterActionFuture.actionGet(AdapterActionFuture.java:50)  
at esclient.ESClient.buildQuery(ESClient.java:119)  
at esclient.ESClient.main(ESClient.java:321)  
Caused by: java.lang.NullPointerException  
at  
org.elasticsearch.action.search.SearchRequest.writeTo(SearchRequest.java:541)  
at  
org.elasticsearch.action.search.MultiSearchRequest.writeTo(MultiSearchRequest.java:271)  
at  
org.elasticsearch.transport.netty.NettyTransport.sendRequest(NettyTransport.java:601)  
at  
org.elasticsearch.transport.TransportService.sendRequest(TransportService.java:206)  
at  
org.elasticsearch.action.TransportActionNodeProxy.execute(TransportActionNodeProxy.java:68)  
at  
org.elasticsearch.client.transport.support.InternalTransportClient$2.doWithNode(InternalTransportClient.java:108)  
at  
org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:219)  
at  
org.elasticsearch.client.transport.support.InternalTransportClient.execute(InternalTransportClient.java:105)  
at  
org.elasticsearch.client.support.AbstractClient.multiSearch(AbstractClient.java:360)  
at  
org.elasticsearch.client.transport.TransportClient.multiSearch(TransportClient.java:441)  
at  
org.elasticsearch.action.search.MultiSearchRequestBuilder.doExecute(MultiSearchRequestBuilder.java:78)  
at  
org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:91)  
at  
org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:65)

I have searched my best and you might notice that the above code is the  
sample snippet from ES doc page. Please help me in figuring out what the  
issue is.

P.S:- Individually both those SearchRequestBuilders are working fine.  
Best,

V. Varadhan

--  
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/badbc70d-6c2f-4f63-bb99-dec42addbfec%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/badbc70d-6c2f-4f63-bb99-dec42addbfec%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Veerapuram\_Varadhan](https://avatars.discourse-cdn.com/v4/letter/v/45deac/32.png) [@Veerapuram\_Varadhan](https://discuss.elastic.co/u/Veerapuram_Varadhan)
#### Post date: [December 13, 2014, 5:32pm UTC](https://discuss.elastic.co/t/multisearchrequest-builder-throws-nullpointerexception-in-elasticsearch-1-3-2/21243/2 "2014-12-13T17:32:50Z")

</div>

That was so stupid of me forgetting to set the index and type params.!!  
Looking at the SearchRequest.java:541 struck a big hammer on my head.!

Issue solved. Sorry for the false alarm.

Thanks,

V. Varadhan

On Saturday, December 13, 2014 10:35:18 PM UTC+5:30, Veerapuram Varadhan  
wrote:

> Hi,
> 
> If this is already answered, please point me to the thread. TIA for that.
> 
> Now, the problem:
> 
> I am having the following code to test MultiSearchRequest{Buuilder} and it  
> always throws me NullPointerException (even if I just use matchAllQuery()  
> in the SearchRequests added to it).
> 
> public void performMultiSearchQuery () {  
> Client client = new TransportClient().addTransportAddress(new  
> InetSocketTransportAddress("localhost", 9300));  
> SearchRequestBuilder srb1 = client.prepareSearch()  
> .setQuery(QueryBuilders.queryString("sev:1")).setSize(Integer.MAX\_VALUE);  
> SearchRequestBuilder srb2 = client.prepareSearch()
> 
> .setQuery(QueryBuilders.queryString("priority:50")).setSize(Integer.MAX\_VALUE);
> 
> MultiSearchResponse sr = client.prepareMultiSearch()  
> .add(srb1.request())  
> .add(srb2.request())  
> .execute().actionGet();
> 
> // You will get all individual responses from  
> MultiSearchResponse#getResponses()  
> long nbHits = 0;  
> for (MultiSearchResponse.Item item : sr.getResponses()) {  
> SearchResponse response = item.getResponse();  
> nbHits += response.getHits().getTotalHits();  
> }  
> logger.info("Total Number of Hits: " + nbHits);  
> client.close();  
> }
> 
> And the exception it throws is:
> 
> Exception in thread "main"  
> org.elasticsearch.common.util.concurrent.UncategorizedExecutionException:  
> Failed execution  
> at  
> org.elasticsearch.action.support.AdapterActionFuture.rethrowExecutionException(AdapterActionFuture.java:90)  
> at  
> org.elasticsearch.action.support.AdapterActionFuture.actionGet(AdapterActionFuture.java:50)  
> at esclient.ESClient.buildQuery(ESClient.java:119)  
> at esclient.ESClient.main(ESClient.java:321)  
> Caused by: java.lang.NullPointerException  
> at  
> org.elasticsearch.action.search.SearchRequest.writeTo(SearchRequest.java:541)  
> at  
> org.elasticsearch.action.search.MultiSearchRequest.writeTo(MultiSearchRequest.java:271)  
> at  
> org.elasticsearch.transport.netty.NettyTransport.sendRequest(NettyTransport.java:601)  
> at  
> org.elasticsearch.transport.TransportService.sendRequest(TransportService.java:206)  
> at  
> org.elasticsearch.action.TransportActionNodeProxy.execute(TransportActionNodeProxy.java:68)  
> at  
> org.elasticsearch.client.transport.support.InternalTransportClient$2.doWithNode(InternalTransportClient.java:108)  
> at  
> org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:219)  
> at  
> org.elasticsearch.client.transport.support.InternalTransportClient.execute(InternalTransportClient.java:105)  
> at  
> org.elasticsearch.client.support.AbstractClient.multiSearch(AbstractClient.java:360)  
> at  
> org.elasticsearch.client.transport.TransportClient.multiSearch(TransportClient.java:441)  
> at  
> org.elasticsearch.action.search.MultiSearchRequestBuilder.doExecute(MultiSearchRequestBuilder.java:78)  
> at  
> org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:91)  
> at  
> org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:65)
> 
> I have searched my best and you might notice that the above code is the  
> sample snippet from ES doc page. Please help me in figuring out what the  
> issue is.
> 
> P.S:- Individually both those SearchRequestBuilders are working fine.  
> Best,
> 
> V. Varadhan

--  
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/b02c98f2-7df3-48e0-a0ba-b1d71967ae69%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b02c98f2-7df3-48e0-a0ba-b1d71967ae69%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [December 13, 2014, 5:36pm UTC](https://discuss.elastic.co/t/multisearchrequest-builder-throws-nullpointerexception-in-elasticsearch-1-3-2/21243/3 "2014-12-13T17:36:36Z")

</div>

I think a NPE is always bad.  
Could you check if this happens with 1.4.1 as well and if so, open an issue?

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

> Le 13 déc. 2014 à 18:32, Veerapuram Varadhan [v.varadhan@gmail.com](mailto:v.varadhan@gmail.com) a écrit :
> 
> That was so stupid of me forgetting to set the index and type params.!! Looking at the SearchRequest.java:541 struck a big hammer on my head.!
> 
> Issue solved. Sorry for the false alarm.
> 
> Thanks,
> 
> V. Varadhan
> 
> > On Saturday, December 13, 2014 10:35:18 PM UTC+5:30, Veerapuram Varadhan wrote:  
> > Hi,
> > 
> > If this is already answered, please point me to the thread. TIA for that.
> > 
> > Now, the problem:
> > 
> > I am having the following code to test MultiSearchRequest{Buuilder} and it always throws me NullPointerException (even if I just use matchAllQuery() in the SearchRequests added to it).
> > 
> > public void performMultiSearchQuery () {  
> > Client client = new TransportClient().addTransportAddress(new InetSocketTransportAddress("localhost", 9300));  
> > SearchRequestBuilder srb1 = client.prepareSearch()  
> > .setQuery(QueryBuilders.queryString("sev:1")).setSize(Integer.MAX\_VALUE);  
> > SearchRequestBuilder srb2 = client.prepareSearch()  
> > .setQuery(QueryBuilders.queryString("priority:50")).setSize(Integer.MAX\_VALUE);
> > 
> > ```
> > MultiSearchResponse sr = client.prepareMultiSearch()
> > .add(srb1.request())
> > .add(srb2.request())
> > .execute().actionGet();
> > 
> > // You will get all individual responses from MultiSearchResponse#getResponses()
> > long nbHits = 0;
> > for (MultiSearchResponse.Item item : sr.getResponses()) {
> > SearchResponse response = item.getResponse();
> > nbHits += response.getHits().getTotalHits();
> > }
> >   	
> > logger.info("Total Number of Hits: " + nbHits);
> > client.close();
> > 
> > ```
> > 
> > }
> > 
> > And the exception it throws is:
> > 
> > Exception in thread "main" org.elasticsearch.common.util.concurrent.UncategorizedExecutionException: Failed execution  
> > at org.elasticsearch.action.support.AdapterActionFuture.rethrowExecutionException(AdapterActionFuture.java:90)  
> > at org.elasticsearch.action.support.AdapterActionFuture.actionGet(AdapterActionFuture.java:50)  
> > at esclient.ESClient.buildQuery(ESClient.java:119)  
> > at esclient.ESClient.main(ESClient.java:321)  
> > Caused by: java.lang.NullPointerException  
> > at org.elasticsearch.action.search.SearchRequest.writeTo(SearchRequest.java:541)  
> > at org.elasticsearch.action.search.MultiSearchRequest.writeTo(MultiSearchRequest.java:271)  
> > at org.elasticsearch.transport.netty.NettyTransport.sendRequest(NettyTransport.java:601)  
> > at org.elasticsearch.transport.TransportService.sendRequest(TransportService.java:206)  
> > at org.elasticsearch.action.TransportActionNodeProxy.execute(TransportActionNodeProxy.java:68)  
> > at org.elasticsearch.client.transport.support.InternalTransportClient$2.doWithNode(InternalTransportClient.java:108)  
> > at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:219)  
> > at org.elasticsearch.client.transport.support.InternalTransportClient.execute(InternalTransportClient.java:105)  
> > at org.elasticsearch.client.support.AbstractClient.multiSearch(AbstractClient.java:360)  
> > at org.elasticsearch.client.transport.TransportClient.multiSearch(TransportClient.java:441)  
> > at org.elasticsearch.action.search.MultiSearchRequestBuilder.doExecute(MultiSearchRequestBuilder.java:78)  
> > at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:91)  
> > at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:65)
> > 
> > I have searched my best and you might notice that the above code is the sample snippet from ES doc page. Please help me in figuring out what the issue is.
> > 
> > P.S:- Individually both those SearchRequestBuilders are working fine.  
> > Best,
> > 
> > V. Varadhan
> 
> --  
> 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/b02c98f2-7df3-48e0-a0ba-b1d71967ae69%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b02c98f2-7df3-48e0-a0ba-b1d71967ae69%40googlegroups.com).  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/70B365A1-98D0-46EC-B3CE-391BB35E4912%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/70B365A1-98D0-46EC-B3CE-391BB35E4912%40pilato.fr).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![javid](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@javid](https://discuss.elastic.co/u/javid)
#### Post date: [December 13, 2016, 5:13pm UTC](https://discuss.elastic.co/t/multisearchrequest-builder-throws-nullpointerexception-in-elasticsearch-1-3-2/21243/4 "2016-12-13T17:13:04Z")

</div>

it is almost 2017 and I get NPE 🙂

I get NPE when I want to get nbHits, and I cannot get the value of any other field. But, when I disable this line, I get responses without error.  
nbHits += response.getHits().getTotalHits();`

there is my whole code :

```
    > int i = 0;
> ArrayList<SearchRequestBuilder> srb = new ArrayList<SearchRequestBuilder>(formulae.size());

> Client client = TransportClient.builder().build()
> .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("localhost",9300)));
>     		
> MultiSearchRequestBuilder sr = client.prepareMultiSearch();
> for (String formula : formulae) {
> srb.add(client.prepareSearch(index).setSource(formula));
> sr.add(srb.get(i));
> i += 1;
> } 
> MultiSearchResponse resp = sr.execute().actionGet(); 
>     	
>     				
> long nbHits = 0;
> for (MultiSearchResponse.Item item : resp.getResponses()){
> SearchResponse response = item.getResponse();
>     			 
> nbHits += response.getHits().getTotalHits(); 
>     			
>     		 
> System.out.println(response);
>     		
> } 
> client.close();
```

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [December 13, 2016, 5:38pm UTC](https://discuss.elastic.co/t/multisearchrequest-builder-throws-nullpointerexception-in-elasticsearch-1-3-2/21243/5 "2016-12-13T17:38:49Z")

</div>

Would be better to open a new discussion.

Also remove `>` in your code snippet. Just use `</>` to format code.

---

<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 5, 2017, 10:05pm UTC](https://discuss.elastic.co/t/multisearchrequest-builder-throws-nullpointerexception-in-elasticsearch-1-3-2/21243/6 "2017-07-05T22:05:00Z")

</div>


