1

I am trying to get a TTS working in netbeans for java. I have unzipped the JSAPI files and added the following jars to the library

  • cmu_us_kal.jar
  • cmulex.jar
  • en_us.jar
  • freetts.jar
  • cmulex.jar
  • jsapi.jar

When I run the follow code:

package demofreetts2;

import javax.speech.*;
import java.util.*;
import javax.speech.synthesis.*; 

public class Demofreetts2 {

String speaktext ="";
public void doSpeak(String speak, String voicename)
 {
   speaktext = speak;
   String voiceName = voicename;
   try
    {
    SynthesizerModeDesc desc = new SynthesizerModeDesc(null, "general", Locale.US, null, null);
     Synthesizer synthesizer = Central.createSynthesizer(desc);
     synthesizer.allocate();
     synthesizer.resume();
     desc = (SynthesizerModeDesc)synthesizer.getEngineModeDesc();
     Voice[] voices = desc.getVoices();
     Voice voice = null;
     for(int i = 0; i< voices. length; i++)
      {
       if(voices[i].getName().equals(voiceName))
        {
         voice = voices[i];
         break;
        }
      }
     synthesizer.getSynthesizerProperties().setVoice(voice);
     synthesizer.speakPlainText(speaktext, null);
     synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
     synthesizer.deallocate();
    }
   catch (Exception e)
    {
    String message = "missing speech properties in " + System.getProperty("user.home") + "\n";
        System.out.println (""+e);
    }
 }
public static void main (String[]args)
{
 Demofreetts2 obj = new Demofreetts2();
  obj.doSpeak(args[0],"kevin16");
 }
}

I get the following message:

**run:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at demofreetts2.Demofreetts2.main(Demofreetts2.java:46)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)**

When I change main method like so:

public static void main (String[]args)
{
 Demofreetts2 obj = new Demofreetts2();
 obj.doSpeak("hello","kevin16");
}

I get this message:

**run:
java.lang.NullPointerException
BUILD SUCCESSFUL (total time: 1 second)**

Does anyone know how I can get this to work?

I just what a program that will convert a string to speech.

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
user1870404
  • 31
  • 1
  • 4

1 Answers1

0

Error #1 : you'll have to pass some arguments to your main method to use args[0]. See What is "String args[]"? parameter in main method Java for extra information.

Error #2 : you hit a NullPointerException in your code, meaning you try to use a method over an instance which is equal to null. Either use a debugger, or expand you debug info by using e.printStackTrace(); instead of System.out.println (""+e);, in order to know which instance is the culprit.

Community
  • 1
  • 1
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
  • When I adjusted for error2 like you said I got the following message- -------------------------------------------------------------------- run: java.lang.NullPointerException at demofreetts2.Demofreetts2.doSpeak(Demofreetts2.java:19) at demofreetts2.Demofreetts2.main(Demofreetts2.java:46) BUILD SUCCESSFUL (total time: 1 second) ---------------------------------------------------------------- Is this because the voice is a null value? – user1870404 Dec 02 '12 at 19:48
  • What is line 19 of `Demofreetts2`? One of the variables on this line is `null`. – Alexis Pigeon Dec 02 '12 at 20:55
  • So you'll have to check why `Central.createSynthesizer(desc)` returns `null`, since that's the cause of the `NullPointerException` on the line after... – Alexis Pigeon Dec 02 '12 at 22:53