我对 Java 编程很陌生,并且一直通过在线阅读和到处购买一些 Java 编程书籍来自学。
但是,在构建我已经工作了几周的语音识别程序时,我遇到了一个问题。它给了我一条nullpointer exception at the rec.addresultlistener
编码线,我完全不知道为什么,因为据我所知,这条线告诉程序要监听输入,以便根据语法检查它。该程序以前给我的 rec.allocate()
编码行相同的错误,但通过将其声明为空来解决。我是否也应该将其声明为 null ?
有人可以在这里帮助我并解释原因吗?
//Create a recognizer that supports English
rec = Central.createRecognizer( new EngineModeDesc (Locale.ENGLISH));;
//Start up the recognizer
if(rec!=null){
rec.allocate();
//Load the grammar from a file, and enable it
System.out.println(new File(".").getAbsoluteFile());
FileReader reader = new FileReader("grammar2.0");
if (reader != null) System.out.println("no file");;
System.out.println(reader);
RuleGrammar gram = rec.loadJSGF(reader);
gram.setEnabled(true);
}
//Add the listener to get results
rec.addResultListener(new speech());
//commit the grammar
rec.commitChanges();
//Request focus and start listening
rec.requestFocus();
rec.resume();