Reputation: 31
I have been trying to create a constructor of class use Microsoft Cognitive and AI toolkit with QnAMaker API, in order to create a simplistic chat bot.
While my normal qnaMakerAi chat bot works fine, there is an issue while I was trying to enhance it's feature and include the Constructor. and while running the code in emulator I'm getting below error:
message: Sorry, my bot code is having an issue. POST:500 directline.postActivity
The issue I'm having is: "Object reference not set to an instance of an object."
Please check and suggest.
Based on the user comments, here is the code for RootDialog - `
namespace Microsoft.Bot.Sample.QnABot {
[Serializable]
public class RootDialog : QnAMakerDialog //IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
/* Wait until the first message is received from the conversation and call MessageReceviedAsync
* to process that message. */
context.Wait(this.MessageReceivedAsync);
}
public RootDialog() : base(new QnAMakerService(new QnAMakerAttribute(ConfigurationManager.AppSettings["QnASubscriptionKey"], ConfigurationManager.AppSettings["QnAKnowledgebaseId"], "Sorry, I couldn't find an answer for that")))
{
}
protected override async Task RespondFromQnAMakerResultAsync(IDialogContext context, IMessageActivity message, QnAMakerResults result)
{
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
/* When MessageReceivedAsync is called, it's passed an IAwaitable<IMessageActivity>. To get the message,
* await the result. */
var message = await result;
// var qnaAuthKey = Utils.GetAppSetting("QnAAuthKey");
var qnaAuthKey = GetSetting("QnAAuthKey");
// var qnaKBId = Utils.GetAppSetting("QnAKnowledgebaseId");
// var endpointHostName = Utils.GetAppSetting("QnAEndpointHostName");
var qnaKBId = ConfigurationManager.AppSettings["QnAKnowledgebaseId"];
var endpointHostName = ConfigurationManager.AppSettings["QnAEndpointHostName"];
// QnA Subscription Key and KnowledgeBase Id null verification
if (!string.IsNullOrEmpty(qnaAuthKey) && !string.IsNullOrEmpty(qnaKBId))
{
// Forward to the appropriate Dialog based on whether the endpoint hostname is present
if (string.IsNullOrEmpty(endpointHostName))
await context.Forward(new BasicQnAMakerPreviewDialog(), AfterAnswerAsync, message, CancellationToken.None);
else
await context.Forward(new BasicQnAMakerDialog(), AfterAnswerAsync, message, CancellationToken.None);
}
else
{
await context.PostAsync("Please set QnAKnowledgebaseId, QnAAuthKey and QnAEndpointHostName (if applicable) in App Settings. Learn how to get them at https://aka.ms/qnaabssetup.");
}
}
private async Task AfterAnswerAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
// wait for the next user message
context.Wait(MessageReceivedAsync);
}
public static string GetSetting(string key)
{
var value = ConfigurationManager.AppSettings[key];
if (String.IsNullOrEmpty(value) && key == "QnAAuthKey")
{
value = ConfigurationManager.AppSettings["QnASubscriptionKey"]; // QnASubscriptionKey for backward compatibility with QnAMaker (Preview)
}
return value;
}
}
// Dialog for QnAMaker Preview service
[Serializable]
public class BasicQnAMakerPreviewDialog : QnAMakerDialog
{
// Go to https://qnamaker.ai and feed data, train & publish your QnA Knowledgebase.
// Parameters to QnAMakerService are:
// Required: subscriptionKey, knowledgebaseId,
// Optional: defaultMessage, scoreThreshold[Range 0.0 – 1.0]
public BasicQnAMakerPreviewDialog() : base(new QnAMakerService(new QnAMakerAttribute(RootDialog.GetSetting("QnAAuthKey"), ConfigurationManager.AppSettings["QnAKnowledgebaseId"], "No good match in FAQ.", 0.5)))
{ }
}
// Dialog for QnAMaker GA service
[Serializable]
public class BasicQnAMakerDialog : QnAMakerDialog
{
// Go to https://qnamaker.ai and feed data, train & publish your QnA Knowledgebase.
// Parameters to QnAMakerService are:
// Required: qnaAuthKey, knowledgebaseId, endpointHostName
// Optional: defaultMessage, scoreThreshold[Range 0.0 – 1.0]
public BasicQnAMakerDialog() : base(new QnAMakerService(new QnAMakerAttribute(RootDialog.GetSetting("QnAAuthKey"), ConfigurationManager.AppSettings["QnAKnowledgebaseId"], "No good match in FAQ.", 0.5, 1, ConfigurationManager.AppSettings["QnAEndpointHostName"])))
{ }
}}
`
For RootDialog -
Upvotes: 0
Views: 752
Reputation: 29
if you are creating any objects out side of methods, try to create the object inside the method. for example
class1 myclass1 = new class1();
public string void MyMethod()
{
}
//above line might be an error.
//do it as
public string void MyMethod()
{
class1 myclass1 = new class1();
}
Upvotes: 0
Reputation: 27793
The issue I'm having is: "Object reference not set to an instance of an object."
Based on the code that you provided, I can reproduce the issue. It seems that you create Bot service with Question and Answer(c#) template and download the source code from Azure portal, and then you modify the code to make the RootDialog inherit QnAMakerDialog
class. As Eric Dahlvang mentioned in comment, if you check the definition of QnAMakerDialog
, you will find the implementation of your RootDialog is invalid.
Besides, based on my test, if you use QnAAuthKey and QnAKnowledgebaseId of GA QnAMaker service to initate your QnAMakerDialog, but not provide QnAEndpointHostName, the exception will be thorwn.
Note: For more information about GA QnAMaker service, you can refer to this blog.
Upvotes: 1
Reputation: 280
I am not able to understand where exactly you are getting the error.
Just for a suggestion you may try like this too
using Microsoft.Bot.Builder.CognitiveServices.QnAMaker;
namespace Test.Qna
{
[Serializable]
[QnAMaker(authKey: "AuthKey", knowledgebaseId: "KnowledgebaseId", defaultMessage: "please rephrase, I could not understand.", scoreThreshold: 0.5, top: 1, endpointHostName: "https://yourAccount.azurewebsites.net/qnamaker")]
public class QnADialog : QnAMakerDialog
{
}
}
Upvotes: 0