2012-04-29 69 views
-2

我試圖將語音轉換爲文本並將其顯示在AWT textArea上。 但語音到文本轉換器函數的輸出是在while循環內生成的,我無法在textArea上顯示它。我得到一個空指針異常;請有人幫忙。顯示循環到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

man ...當然你得到一個空指針異常。 textArea爲空,不是嗎? textarea和textArea是有區別的。你有兩個文本區域:一個在你的主要方法中(這是你最初的問題),另一個在你沒有初始化的地方。所以它是空的。 –

回答

1

將您的文本區域聲明爲類中的私有靜態字段。然後,你可以很容易地在像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); 
     } 
} 

訪問它在下一步中,你應該將創建你的UI到您的類的構造函數的所有代碼。這將是一個更清潔的解決方案。

+0

感謝您的回覆。 – user1296187

+0

如果它解決了您的問題投票並將其標記爲正確答案;) –

+0

他永遠不會再回到這裏。 – SHiRKiT

0

由於識別器運行,直到您退出程序。如果你想更新textarea,你必須在一個可以與Gui交互的線程中設置你的更新代碼。例。

public void update(String s){ 
    SwingUtilities.invokeLater(new Runnable(){ 
     public void run(){ 
      textArea.append(s); 
       }// end run 
      });//end Runnable 
       }// end method