我正在将Spinx语音识别作为一个线程运行,并希望将结果传递给一个bot方法,该方法镜像Main中的相同方法来传递文本。然而,继续运行NullPointerExpection并且无法推断或克服它。为什么它在Main中有效,但在Reply类中无效。
Main中的文本发送按钮是:
private void onSend(java.awt.event.ActionEvent evt) {
// display users message
txtHistory.setText(txtHistory.getText() + "\nYou: " + txtMessage.getText() + "\n");
//send the input to the bot and get bot response
String response = bot.send(txtMessage.getText());
//if the response is not empty show it
if (response.length() > 0) {
addBotText(response);
}
//display new state message to continue
addBotText(bot.getMessage());
//clear the input message box
txtMessage.setText("");
}
在Runnable(Threaded)Spinx Reply类中,我有一个方法来获取语音输入,然后将其显示为字符串:
private void start_recognition(Result result) {
if (result != null) {
String resultText = result.getBestFinalResultNoFiller();
//send the message to the bot
String response = bot.send(resultText);
System.out.println("You said: " + resultText + '\n');
} else {
System.out.println("I can't hear what you said.\n");
}
}
(使用私人Bot机器人;在Main和Reply中都有评论) 当我评论String response = bot.send(resultText); out all is well,但是尝试调用bot.send方法会创建一个nullPointerException。我意识到我正在做一些令人难以置信的愚蠢但却无法理解的事情。 Bot方法如下。非常感谢任何帮助。
在Bot类中发送方法:
public String send(String message) {
String response = "";
State state = parser.getState(level);
// end of the tree
if (state.getKeywords().isEmpty()) {
this.level = "1";
}
// match the keyword with given message
Keyword match = parse(message, state.getKeywords());
// if no keyword is matched, display one of the invalid answers
if (match == null) {
response = parser.getInvalidAnswer();
} else {
// if match classname is provided, check to get the dynamic response
if (match.className.length() > 0) {
// check for Weather dynamic response
if (match.className.equals("Weather")) {
Weather weather = new Weather();
response = weather.getResponse(match.arg);
this.level = "1";
}
// check for News dynamic response
if (match.className.equals("News")) {
News news = new News();
response = news.getResponse(match.arg);
this.level = "1";
}
} else {
// get the new state and return the new message
if (response.length() == 0) {
this.level = match.target;
state = parser.getState(level);
// if it is end of the tree
if (state.getKeywords().isEmpty()) {
response = this.getMessage();
this.level = "1";
}
}
}
}
return response;
}
条件机器人的所有代码都归功于Majid Khosravi