Speech recognition in java

I want to use speech recognition in my project, and I found this code, but when I run it, I get an error message:

run: java.lang.NullPointerException
        at newpackage.HelloWorld.main(HelloWorld.java:55)

Please, could any of you help me in this problem?

This is the server code I use:

package newpackage;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.speech.*;
import javax.speech.recognition.*;
import java.io.FileReader;
import java.util.Locale;

public class HelloWorld extends ResultAdapter {
  static Recognizer rec;

  // Receives RESULT_ACCEPTED event: print it, clean up, exit
  public void resultAccepted(ResultEvent e) {
    Result r = (Result)(e.getSource());
    ResultToken tokens[] = r.getBestTokens();

    for (int i = 0; i < tokens.length; i++)
      System.out.print(tokens[i].getSpokenText() + " ");

    System.out.println();
    try {
          // Deallocate the recognizer and exit
          rec.deallocate();
    } catch (EngineException ex) {
          Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
    } catch (EngineStateError ex) {
          Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.exit(0);
  }

  public static void main(String args[]) {
    try {
      // Create a recognizer that supports English.
      rec = Central.createRecognizer(
              new EngineModeDesc(Locale.ENGLISH));

      // Start up the recognizer
      rec.allocate();

      // Load the grammar from a file, and enable it
      FileReader reader = new FileReader(args[0]);
      RuleGrammar gram = rec.loadJSGF(reader);

      gram.setEnabled(true);

      // Add the listener to get results
      rec.addResultListener(new HelloWorld());

      // Commit the grammar
      rec.commitChanges();

      // Request focus and start listening
      rec.requestFocus();
      rec.resume();
    } catch (Exception e) {
      e.printStackTrace();
          // System.out.println("the problem");
    }
  }
}
+3
source share
2 answers
if (rec != null) {
    System.out.println(rec);
}
else {
    System.out.println("rec is null");   
    // <-- here your problem.  you need to return, exit, or throw here!
}

// Start up the recognizer
rec.allocate();  // <-- This is the line that blowing out (I assume)

You will get a null pointer, because although you are still handling the case where recit is null, you continue the program. You need to return or exit, or something, when recit is null.

: , , if/else. if/else, . .

: , createRecognizer null, , .

+2

// Create a recognizer that supports English.
      rec = Central.createRecognizer(
              new EngineModeDesc(Locale.ENGLISH));

            SynthesizerModeDesc desc = new SynthesizerModeDesc(
            null,          // engine name
            "general",     // mode name
            Locale.US,     // locale
            null,          // running
            null);         // voice

     synth = Central.createSynthesizer(desc);
+2

Source: https://habr.com/ru/post/1706752/


All Articles