Reputation: 2932
I am trying to add search fields to my Azure Search query (see below instantiation of the SearchParameters object).
public async Task StartAsync(IDialogContext context)
{
ISearchIndexClient indexClient = CreateSearchIndexClient();
try
{
Trace.TraceInformation($"Starting StartAsync");
SearchParameters searchParameters = new SearchParameters();
searchParameters.SearchFields.Add("StoreNumber");
searchParameters.SearchFields.Add("StoreName");
Trace.TraceInformation($"Finished adding search fields");
// Trace.TraceInformation($"Search Parameters added = {searchParameters.SearchFields.Count}");
DocumentSearchResult results = await indexClient.Documents.SearchAsync(searchText, searchParameters);
Trace.TraceInformation($"results obtained");
List<SearchHit> searchHits = results.Results.Select(r => ResultMapper.ToSearchHit(r)).ToList();
Trace.TraceInformation($"search hits {searchHits.Count}");
await SendResultsOfSearch(context, results);
}
catch (Exception ex)
{
Trace.TraceError($"Exception {ex.ToString()}");
}
}
For some reason it's throwing the following exception but I have no idea why?
2018-09-03T00:47:39 PID[3268] Information Starting StartAsync
2018-09-03T00:47:39 PID[3268] Error Exception System.NullReferenceException: Object reference not set to an instance of an object.
at LuisBot.Dialogs.SearchRBMDialog.<StartAsync>d__2.MoveNext() in C:\Users\jmatson\Downloads\retail-info-bot-v2-src\Dialogs\SearchRBMDialog.cs:line 32
The code compiles fine? And there are no constructor arguments required as far as I know.
Upvotes: 1
Views: 589
Reputation: 8634
SearchFields
is not initialized by the SearchParameters
constructor (see the source code here), so calling Add
on it will result in NullReferenceException
. It's of type IList
, so the easiest way to initialize it is by assigning an array to it, like this:
searchParameters.SearchFields = new[] { "StoreNumber", "StoreName" };
Upvotes: 1
Reputation: 14619
Several possibilities (depending on your objects implementation):
Here:
SearchParameters searchParameters = new SearchParameters();
searchParameters.SearchFields.Add("StoreNumber");
searchParameters.SearchFields.Add("StoreName");
Is SearchFields
a List
? Was it initialized during SearchParameters
construction? If not, in that case you can have a NullReferenceException
when you add items to something non instantiated.
Or here:
List<SearchHit> searchHits = results.Results.Select(r => ResultMapper.ToSearchHit(r)).ToList();
In your results
object, are you sure that Results
is instantiated? If not, NullReferenceException
To find which line throws the Exception, look at line 32 in your code (the line number is in your error log)
Upvotes: 0