# NullPointerException in MultiSearch API

**URL:** https://discuss.elastic.co/t/nullpointerexception-in-multisearch-api/69056
**Category:** Elasticsearch
**Created:** [December 14, 2016, 5:20pm UTC](https://discuss.elastic.co/t/nullpointerexception-in-multisearch-api/69056 "2016-12-14T17:20:47Z")
**Posts on this page:** 2
**Page:** 1

<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 14, 2016, 5:20pm UTC](https://discuss.elastic.co/t/nullpointerexception-in-multisearch-api/69056/1 "2016-12-14T17:20:47Z")

</div>

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 :

```
public static void requestBuilder(ArrayList<String> formulae) {
		
		ArrayList<SearchRequestBuilder> srb = new ArrayList<SearchRequestBuilder>(formulae.size());	
			
		
		Client client = TransportClient.builder().build()
				.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("localhost",9300))); 
	
	int i = 0;
		
		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(); 
	  		
	}

```

But when I use the following code, I can get get results from any field:

```
SearchResponse response = client.prepareSearch()
				    .setIndices(index)
				    .setSource(formulae.get(1))			
				    .execute()
				    .actionGet();
						
					for (SearchHit hit : response.getHits()) {
						String get = hit.getSource().get("text").toString();
				       	System.out.println(get);
					}

```

How can I do the same with MultiSearch Api?

---

<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: [January 11, 2017, 5:21pm UTC](https://discuss.elastic.co/t/nullpointerexception-in-multisearch-api/69056/2 "2017-01-11T17:21:23Z")

</div>

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