2012-04-29 6 views
-2

私は音声をテキストに変換し、AWT textAreaに表示しようとしています。 しかし、スピーチからテキストへの変換関数の出力はwhileループの中で生成され、textAreaに表示することができません。私は、NULLポインタの例外を取得しています。誰か助けてください。内部にある変数の値をTextAreaにループ中に表示

public class Speechrec { 

    private static TextArea textArea; 
    String resultText; 
    private String dr; 

    public void recognizer(String[] args) { 

     try { 
      URL url; 
      if (args.length > 0) { 
       url = new File(args[0]).toURI().toURL(); 
      } 
      else { 
       url = Speechrec.class.getResource("speechrec.config.xml"); 
      }  
      System.out.println("Loading...");  
      ConfigurationManager cm = new ConfigurationManager(url);  
      Recognizer recognizer = (Recognizer) cm.lookup("recognizer"); 
      Microphone microphone = (Microphone) cm.lookup("microphone");  
      /* allocate the resource necessary for the recognizer */ 
      recognizer.allocate();  
      /* the microphone will keep recording until the program exits */ 
      if (microphone.startRecording()) {  
      System.out.println("Say: some greetings"); 
      while (true) { 
       System.out.println("Start speaking. Press Ctrl-C to quit.\n"); 

       Result result = recognizer.recognize(); 

       if (result != null) { 
        String resultText = result.getBestFinalResultNoFiller();    
        textArea.setText(resultText); 
       } 
       else { 
        System.out.println("I can't hear what you said.\n"); 
       } 
      } 
      } 
      else { 
       System.out.println("Cannot start microphone."); 
       recognizer.deallocate(); 
       System.exit(1); 
      } 
     } 
     catch (Exception) {     
      // exception handling 
     } 
    }   

public static void main(final String[] args) throws IOException { 
    Speechrec sp1=new Speechrec();  
    Frame frame=new Frame("speech to sign language converter"); 
    TextArea textarea=new TextArea (05,30);  
    Button button = new Button("Start speaking"); 
    // ...   
    frame.add(button,BorderLayout.SOUTH); 
    // ... 
    frame.setLayout(new FlowLayout(FlowLayout.TRAILING,50,15)); 
    frame.setSize(500,400); 
    frame.setVisible(true); 

    button.addActionListener(new ActionListener() {   
     public void actionPerformed(ActionEvent e1) {  
      Speechrec sp=new Speechrec(); 
     sp.recognizer(args); 
      }}); 
    }  
} 
+3

*正確に*の質問は何ですか?手元にある*具体的な問題に関連する最小限のコードを提供してください。 – amit

+3

コードの関連する**部分を投稿してください(http://sscce.orgも参照してください)。 –

+0

男...もちろん、あなたはNULLポインタ例外を取得しています。 textAreaはnullですか? textareaとtextAreaには違いがあります。 2つのテキスト領域があります:メインメソッド(最初の問題でした)と初期化されていない外側のテキスト領域です。したがって、それはnullです。 –

答えて

1

テキスト領域をクラスのプライベート静的フィールドとして宣言します。そして、あなたはあなたのクラスのコンストラクタにあなたのUIを作成し、すべてのコードを移動する必要があり、次のステップではtextArea.setText("something");

private static TextArea textArea; 

public static void main(final String[] args) throws IOException{ 
    Speechrec sp1 = new Speechrec(); 
    textArea = new TextArea(sp1.dr,05,30); 
    Button button = new Button("Start speaking"); 
    // and so on... 
} 

while (true) { 

     Result result = recognizer.recognize(); 

     if (result != null) { 

      String resultText = result.getBestFinalResultNoFiller(); 

      dr = resultText; 

      textArea.setText(resultText); 

      // or: 

      textArea.append(resultText); 
     } 
} 

のように簡単にそれをアクセスすることができます。それはよりクリーンなソリューションになります。

+0

返信ありがとうございます。 – user1296187

+0

あなたの問題を解決して正解とマークすれば –

+0

ここに戻ってくることはありません。実際に – SHiRKiT

0

認識プログラムはプログラムを終了するまで実行されるため、テキストエリアを更新する場合は、Guiとやり取りできるスレッドに更新コードを設定する必要があります。例。

public void update(String s){ 
    SwingUtilities.invokeLater(new Runnable(){ 
     public void run(){ 
      textArea.append(s); 
       }// end run 
      });//end Runnable 
       }// end method 
関連する問題