0

I wanted to convert text in a JTextArea into voice but I seem to keep getting an error at the same line.

Voice voice;
    VoiceManager vm = VoiceManager.getInstance();
    voice = vm.getVoice(VOICENAME);

    voice.allocate();
    try{
        voice.speak(voice_txt.getText());
    }
    catch (Exception e){
    JOptionPane.showMessageDialog(rootPane, e);}

This is the error I keep getting

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at FrameTextToSpeech.jButton1ActionPerformed(FrameTextToSpeech.java:75)

The error points to this line

voice.allocate();
T Lin
  • 25
  • 9
  • @Berger tried that I used `voice_txt = new JTextField();` but it didn't work. – T Lin May 19 '16 at 15:16
  • I guess that `getVoice` returns `null` because no voice with this name (VOICENAME) has been found, see here : http://freetts.sourceforge.net/javadoc/com/sun/speech/freetts/VoiceManager.html#getVoice(java.lang.String) . You should call `contains` first, to avoid this kind of problem. – Arnaud May 19 '16 at 15:18
  • U mean by using an `if`statement to check if the text field contains text and then executing `.allocate`? – T Lin May 19 '16 at 15:22
  • No, the problem is before that, `VoiceManager` doesn't find any voice that match the name you pass (the VOICENAME parameter). So `voice` is `null`, and `voice.allocate()` crashes. – Arnaud May 19 '16 at 15:24
  • Still the same error. I used `String VOICENAME="kevin16";` but no change – T Lin May 19 '16 at 15:32
  • `System.out.println(vm);` should print a list of the available voice names . – Arnaud May 19 '16 at 15:37
  • It's empty. I get your point now. How do I add the voices? I mean use a separate jar? – T Lin May 19 '16 at 15:41

0 Answers0