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