0

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

barryhunter
  • 20,886
  • 3
  • 30
  • 43
  • 1
    Can you run it in the debugger and give us the stacktrace of the exception? – x squared Jul 04 '15 at 10:44
  • Hi squared X, the debugger gives: – James O'Connor Jul 04 '15 at 12:07
  • Listening on javadebug User program running Debugger stopped on uncompilable source code. User program finished – James O'Connor Jul 04 '15 at 12:07
  • 1
    The message You've posted, indicates that the debugger cannot do its work, because the exception is thrown before. Fix that first and then post the stacktrace. If your code **does** compile and you still get the exception in the debugger, maybe this discussion will help you? http://stackoverflow.com/questions/4386076/uncompilable-source-code-runtimeexception-in-netbeans – x squared Jul 04 '15 at 16:09
  • 1
    Seems to me that `bot` in `bot.send(resultText);` is `null`, how do you initialize `private Bot bot;`? –  Jul 04 '15 at 18:15

0 Answers0