在運行使用JSAPI基本的HelloWorld程序,它顯示錯誤 「顯示java.lang.NullPointerException在HelloWorld.main(HelloWorld.java:11)」運行時錯誤 - JSAPI HelloWorld程序
以下是代碼:
import javax.speech.*;
import javax.speech.synthesis.*;
import java.util.Locale;
public class HelloWorld {
public static void main(String args[]) {
try {
// Create a synthesizer for English
Synthesizer synth = Central.createSynthesizer(new SynthesizerModeDesc(Locale.ENGLISH));
// Get it ready to speak
synth.allocate();
synth.resume();
// Speak the "Hello world" string
synth.speakPlainText("Hello, world!", null);
// Wait till speaking is done
synth.waitEngineState(Synthesizer.QUEUE_EMPTY);
// Clean up
synth.deallocate();
} catch (Exception e) {
e.printStackTrace();
}
}
}
編輯: 修改我的程序:
import javax.speech.*;
import javax.speech.synthesis.*;
import java.util.Locale;
public class HelloWorld {
public static void main(String args[]) {
try {
// Create a synthesizer for English
SynthesizerModeDesc modeDesc = new SynthesizerModeDesc(null,"general",Locale.US,null,null);
System.out.println(modeDesc);
Synthesizer synth = Central.createSynthesizer(modeDesc);
//Synthesizer synth = Central.createSynthesizer(null);
// Get it ready to speak
System.out.println(synth);
synth.allocate();
synth.resume();
// Speak the "Hello world" string
synth.speakPlainText("Hello, world!", null);
// Wait till speaking is done
synth.waitEngineState(Synthesizer.QUEUE_EMPTY);
// Clean up
synth.deallocate();
} catch (Exception e) {
e.printStackTrace();
}
}
}
和它給輸出:
[email protected]
null
java.lang.NullPointerException
at HelloWorld.main(HelloWorld.java:15)
似乎SynthesizerModeDesc工作正常,但它沒有檢測到任何引擎,因爲我試圖將null也傳遞給函數Central.createSynthesizer(即獲取默認引擎),但它仍然返回null。我檢查了它正在檢測的引擎數量,但它顯示0.
請幫助我! :(
這是完整的stacktrace嗎? – 2012-02-04 21:13:50
是的,這是完整的堆棧跟蹤。 – 2012-02-05 05:33:44