我面臨着一些例外下面的代碼:異常在Java中的語音識別
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();
}
}
}
例外: - 在Demo5.HelloWorld.main(的HelloWorld 異常線程 「main」 顯示java.lang.NullPointerException 。的java:55) Java結果:1個
異常在此行中發生(第55行):
// Start up the recognizer
rec.allocate();
哪裏是代碼中的第55行? – 2014-10-02 03:26:36
爲什麼你定義這個變量是靜態的?靜態識別器rec; – 2014-10-02 03:31:35
我試圖讓這個變量(靜態識別器rec;)非靜態,但它仍然拋出相同的異常。 – King 2014-10-02 03:35:58