1

I am facing some exception in the following code :

    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Locale;
    import javax.speech.AudioException;
    import javax.speech.Central;
    import javax.speech.EngineException;
    import javax.speech.EngineModeDesc;
    import javax.speech.EngineStateError;
    import javax.speech.recognition.GrammarException;
    import javax.speech.recognition.Recognizer;
    import javax.speech.recognition.Result;
    import javax.speech.recognition.ResultAdapter;
    import javax.speech.recognition.ResultEvent;
    import javax.speech.recognition.ResultToken;
    import javax.speech.recognition.RuleGrammar;

    public class HelloWorld extends ResultAdapter {

        static Recognizer rec;

        // Receives RESULT_ACCEPTED event: print it, clean up, exit
        @Override
        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 | EngineStateError ex) {
                System.out.println(ex.toString());
            }
            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 (IllegalArgumentException | EngineException | SecurityException | GrammarException | IOException | AudioException e) {
                e.printStackTrace();
            }
        }
    }

Exception:- Exception in thread "main" java.lang.NullPointerException at Demo5.HelloWorld.main(HelloWorld.java:55) Java Result: 1

Exception occured in this line (line 55):

// Start up the recognizer
 rec.allocate();
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
King
  • 53
  • 1
  • 9

1 Answers1

0

The Recognizer appears to be null. See this line:

rec = Central.createRecognizer(new EngineModeDesc(Locale.ENGLISH));

The docs suggest that this is a problem with the arguments you are providing to createRecognizer:

If there is no Recognizer with the required properties the method returns null.

As for fixing the cause of the null Recognizer, the second answer to this question may be helpful.

Community
  • 1
  • 1
deyur
  • 557
  • 2
  • 14
  • if you mean defiening variable, he has already done that and type is static – Kick Buttowski Oct 02 '14 at 03:33
  • No, the exception is occurring on the line immediately after he assigns `rec` the value of `Central.createRecognizer(new EngineModeDesc(Locale.ENGLISH));`. The problem is with the call to `createRecognizer`. – deyur Oct 02 '14 at 03:38