0

While running basic HelloWorld program using JSAPI, it is showing error "java.lang.NullPointerException at HelloWorld.main(HelloWorld.java:11)"

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 (i.e get default engine) but still it is returning null. I checked number of engines it is detecting, but it shows 0.

Please help me !! :(

3 Answers3

2

From the javadocs for Central.createSynthesizer:

Returns: a Synthesizer matching the required properties or null if none is available

Apparently, there's none available. Perhaps trying Locale.US might produce a valid 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. The constructor you're using (new SynthesizerModeDesc(Locale.ENGLISH)) says,

"Construct an EngineModeDesc for a locale. The engine name, mode name and running are set to 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.

Brian Roach
  • 72,790
  • 10
  • 128
  • 154
1

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" ?

Peter Svensson
  • 5,941
  • 1
  • 29
  • 31
0

you have to make sure you include 'jsapi.jar', 'freetts.jar', and 'jreetts-jsapi10.jar' into your build path. if you dont have those in your freetts directory you have to go into the lib subdirectory and launch jsapi.exe. and also make sure you have 'speech.properties' copied into your C:Users/"yourUserNameHere" directory. hope that helps

user1459976
  • 197
  • 5
  • 14