I'm running Spinx voice recognition as a thread and want to pass the result to a bot method mirroring the same method in Main to pass text. However keep running into NullPointerExpection and can't reason it out or overcome it. Why does it work in Main but not in Reply class.
the text send button in Main is:
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("");
}
In the Runnable (Threaded) Spinx Reply class i have a method to get the voice input and then show it as a string:
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");
}
}
(With private Bot bot; commented above in both Main and Reply) When I comment the String response = bot.send(resultText); out all is well, but trying to invoke the bot.send method creates a nullPointerException. I realize I'm doing something incredibly stupid but can't figure it out. The Bot method is below. Any help greatly appreciated.
In Bot class the send method:
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;
}
All code for the conditional bot is credited to Majid Khosravi