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();