[英]Runtime Error - JSAPI HelloWorld Program
While running basic HelloWorld program using JSAPI, it is showing error "java.lang.NullPointerException at HelloWorld.main(HelloWorld.java:11)" 使用JSAPI运行基本的HelloWorld程序时,它显示错误“ HelloWorld.main(HelloWorld.java:11)上的java.lang.NullPointerException”
Following is the code: 以下是代码:
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();
}
}
}
Edit: I edit my program: 编辑:我编辑我的程序:
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();
}
}
}
and it is giving output: 它正在输出:
javax.speech.synthesis.SynthesizerModeDesc@9304b1
null
java.lang.NullPointerException
at HelloWorld.main(HelloWorld.java:15)
It seems that SynthesizerModeDesc is working fine but it is not detecting any engine as i tried passing null also to the function Central.createSynthesizer (ie get default engine) but still it is returning null. 似乎SynthesizerModeDesc工作正常,但由于我尝试将null也传递给功能Central.createSynthesizer(即获取默认引擎),因此未检测到任何引擎,但仍返回null。 I checked number of engines it is detecting, but it shows 0. 我检查了它正在检测的引擎数量,但显示为0。
Please help me !! 请帮我 !! :( :(
From the javadocs for Central.createSynthesizer
: 从Central.createSynthesizer
的javadocs中:
Returns: a Synthesizer matching the required properties or null if none is available 返回:与所需属性匹配的合成器;如果没有可用的属性,则返回null
Apparently, there's none available. 显然,没有可用的。 Perhaps trying Locale.US
might produce a valid SynthesizerModeDesc
也许尝试Locale.US
可能会产生有效的SynthesizerModeDesc
Edit in response to comment: Seriously, I've never used this thing ... but I'm actually reading the javadocs as we go along here. 编辑以回应评论:认真地讲,我从来没有使用过这个东西……但实际上,我在这里读的是javadocs。 The constructor you're using ( new SynthesizerModeDesc(Locale.ENGLISH)
) says, 您正在使用的构造函数( new SynthesizerModeDesc(Locale.ENGLISH)
)说,
"Construct an EngineModeDesc for a locale. The engine name, mode name and running are set to null". “为语言环境构造EngineModeDesc。引擎名称,模式名称和运行设置为null”。
That doesn't sound good from a "And then I want to use it" perspective. 从“然后我想使用它”的角度来看,这听起来不太好。 There's another constructor that allows you to set those options, and methods to set them as well. 还有另一个构造函数,可让您设置这些选项以及设置它们的方法。
Seems to be a solution discussed here 似乎是这里讨论的解决方案
Synthesizer synth = Central.createSynthesizer(
new SynthesizerModeDesc(
null, // engine name
"general", // mode name
Locale.US, // locale
null, // running
null) // voice
);
Are you sure you've done this "By the way I assume you are using freetts and have copied the "speech.properties" file to your directory of jdk" ? 您确定已经完成了“通过我假设您正在使用freetts并将“ speech.properties”文件复制到jdk目录中”的方法吗?
you have to make sure you include 'jsapi.jar', 'freetts.jar', and 'jreetts-jsapi10.jar' into your build path. 您必须确保在构建路径中包含“ jsapi.jar”,“ freetts.jar”和“ jreetts-jsapi10.jar”。 if you dont have those in your freetts directory you have to go into the lib subdirectory and launch jsapi.exe. 如果您的freetts目录中没有这些文件,则必须进入lib子目录并启动jsapi.exe。 and also make sure you have 'speech.properties' copied into your C:Users/"yourUserNameHere" directory. 并确保已将“ speech.properties”复制到C:Users /“ yourUserNameHere”目录中。 hope that helps 希望能有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.