使用 JSAPI 运行基本 HelloWorld 程序时,显示错误“java.lang.NullPointerException at HelloWorld.main(HelloWorld.java:11)”
以下是代码:
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();
}
}
}
它给出了输出:
javax.speech.synthesis.SynthesizerModeDesc@9304b1
null
java.lang.NullPointerException
at HelloWorld.main(HelloWorld.java:15)
SynthesizerModeDesc 似乎工作正常,但它没有检测到任何引擎,因为我尝试将 null 也传递给函数 Central.createSynthesizer(即获取默认引擎),但它仍然返回 null。我检查了它正在检测的引擎数量,但它显示为 0。
请帮我 !!:(