2014-10-02 88 views
1

我面臨着一些例外下面的代碼:異常在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(); 
+0

哪裏是代碼中的第55行? – 2014-10-02 03:26:36

+0

爲什麼你定義這個變量是靜態的?靜態識別器rec; – 2014-10-02 03:31:35

+0

我試圖讓這個變量(靜態識別器rec;)非靜態,但它仍然拋出相同的異常。 – King 2014-10-02 03:35:58

回答

0

Recognizer顯示爲空。看到這一行:

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

docs表明,這是與你正在爲createRecognizer參數一個問題:如果沒有與所需的性質該方法返回null沒有識別器

至於解決null識別器的原因,第二個答案this question可能會有所幫助。

+0

如果你的意思是defiening變量,他已經這樣做了,而且類型是靜態的 – 2014-10-02 03:33:12

+0

不,這個異常發生在他分配'rec'後立即發生'Central.createRecognizer(new EngineModeDesc(Locale.ENGLISH))的值。 '。問題在於對'createRecognizer'的調用。 – deyur 2014-10-02 03:38:17

+0

你如何建議解決這個問題? – 2014-10-02 03:38:53