Hi, I have been trying to get the Jsapi (Java Speech API) embeded into my web applet but have had quite a few troubles. The script for the Jsapi from what I can see was designed for .Jav files where as I am trying to make a .class file. So my problem is that when I use the following code, although netbeans can view to code fine other than the draw function. But when I export the file and try it in the web browser the web browser repeats the following error hundreds of times:

Exception in thread "AWT-EventQueue-5" java.lang.NullPointerException
	at java.awt.LightweightDispatcher$3.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)

And my code is as follows:

// required when you create an applet
import java.applet.*;
// required to paint on screen
import java.awt.*;

import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
import com.sun.speech.freetts.audio.JavaClipAudioPlayer;




public class voice extends Applet {


    public void init()
        {

        }

// This method gets called when the applet is terminated
// That's when the user goes to another page or exits the browser.
    public void stop()
        {
        // meant be be empty.
        }

// The standard method that you have to use to paint things on screen
    public void paint(Graphics g)
        {
        //in here is the paint data.
        g.drawString("This is a sample",10,20);
        }
    public static void main(String[] args) {


        String voiceName = (args.length > 0)
            ? args[0]
            : "kevin16";


        /* The VoiceManager manages all the voices for FreeTTS.
         */
        VoiceManager voiceManager = VoiceManager.getInstance();
        Voice helloVoice = voiceManager.getVoice(voiceName);

        /* Allocates the resources for the voice.
         */
        helloVoice.allocate();

        /* Synthesize speech.
         */
        helloVoice.speak("Thank you for giving me a voice. "
                         + "I'm so glad to say hello to this world.");

        /* Clean up and leave.
         */
        helloVoice.deallocate();
///        System.exit(0);
    }
}

And the Jsapi is found at http://freetts.sourceforge.net/docs/index.php

I have read that there is some common error with the AWT-EventQueue and the Jsapi but cannot find a solution. Any other java applet text to speech alternatives are also welcome. Please help to solve this problem as I can't find any way around it.

PS: This is my second attempt at the language Java and although Java seems like a great language I just find it hard to find the right functions/code to use.

Try to create .JAR which contains an applet class and JSAPI.

Since nobody else has the answer I had a quick Google, and found this: " How do I use JSAPI in an applet?
It is possible to use JSAPI in an applet. In order to do this, users will need the Java Plug-in (see http://java.sun.com/products/plugin). The reason for this is that JSAPI implementations require access to the AWT EventQueue, and the built-in JDK support in the browsers we've worked with denies any applet access to the AWT EventQueue. The Java Plug-in doesn't have this restriction, and users can configure the Java Plug-in to grant or deny applet access to the AWT Queue. "
(Source: http://java.sun.com/products/java-media/speech/forDevelopers/jsapifaq.html#applet-use ).
If you've already one that, please accept my apologies.

although Java seems like a great language I just find it hard to find the right functions/code to use

Too f'ing right! I reckon a few months to learn 90% of the language, 10 years to learn 50% of the API (if you're lucky).

adatapost>Thanks again JC. You did great job for us.

I just tried multiple classes like adatapost suggested and I haven't yet had any luck but what I am trying to figure out is how to have a combination of .jar and .class files working together so that the browser communicates with the .class file while the system communiticates with the .jar file.
So far I managed to compile the project in 3 .class files (one that lets them communicate) and am trying to make one of them a .jar file. So does anybody know how to make a .jar file in netbeans or even in Java or is there some option inside the compiler instead of some code that needs adding?

Currently my code is as follows:
voice.java

import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
import com.sun.speech.freetts.audio.JavaClipAudioPlayer;

public class voice {
public void main(String[] args) {


        String voiceName = (args.length > 0)
            ? args[0]
            : "kevin16";


        /* The VoiceManager manages all the voices for FreeTTS.
         */
        VoiceManager voiceManager = VoiceManager.getInstance();
        Voice helloVoice = voiceManager.getVoice(voiceName);

        /* Allocates the resources for the voice.
         */
        helloVoice.allocate();

        /* Synthesize speech.
         */
        helloVoice.speak("Thank you for giving me a voice. "
                         + "I'm so glad to say hello to this world.");

        /* Clean up and leave.
         */
        helloVoice.deallocate();
///        System.exit(0);
    }
    }

voicedisplay.java

// required when you create an applet
import java.applet.*;
// required to paint on screen
import java.awt.*;






public class voicedisplay extends Applet {
public class voicedisplayin extends voicedisplay {

    public void init()
        {

        }

// This method gets called when the applet is terminated
// That's when the user goes to another page or exits the browser.
    public void stop()
        {
        // meant be be empty.
        }

// The standard method that you have to use to paint things on screen
    public void paint(Graphics g)
        {
        //in here is the paint data.
        g.drawString("This is a sample",10,20);
        }
}
}

So how do I convert voice.java to a .jar file?

Follow the links what James provided.
To create JAR you can either use IDE build-in function for creating JAR or have look at this tutorial, Creating a JAR file, how to do it manually plus more add-on info

I just checked links and one of them says that it needs to be a jar file while the plugin link links doesn't have much. As for the IDE, I have been using the Netbeans IDE and it appears that it isn't possible to mix .jar files with .class files because the option appears under the project section where it effects all files. And so I guess somehow I am going to have to program an interface between to two compiled Java programs as they would need to be compiled separately. Also, I have found another api called fetival which I will check out but other than that, does anybody know of any other text to speech Java software?

I read that about 2 days ago and the example code seems to be buggy on my compiler and can't seem to load up the online demo. So I guess currently it is only possible for text to speech to be done on .jar files and not .class files. I'll see what else I can get Java to do though as I have only began to learn Java. I think a 2d online game engine would be cool but might be challenging.

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.