roshan_iiita 0 Light Poster

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();
    }
}
}

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 !! :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.