6 Replies Latest reply: Apr 10, 2010 8:02 AM by 843802 RSS

    problem with jsapi code, please help me

    843802
      hey all, hope you help me

      im making a speech recognition application using packeg api

      first I started ith downloding the packeg
      then I add it to my appliction
      and copied a sample code called helloworld

      bu there was a problem


      the code:
       
      
      import javax.speech.*;
      import javax.speech.recognition.*;
      import java.io.FileReader;
      import java.util.Locale;
      
      
      public class HelloWorld extends ResultAdapter {
           static Recognizer rec;
      
      
      
           // Receives RESULT_ACCEPTED event: print it, clean up, exit
           public void resultAccepted(ResultEvent e) {
                Result r = (Result)(e.getSource());
                ResultToken tokens[] = r.getBestTokens();
      
                for (int i = 0; i < tokens.length; i++)
                     System.out.print(tokens.getSpokenText() + " ");
                System.out.println();

                // Deallocate the recognizer and exit
                rec.deallocate();
                System.exit(0);
           }

           public static void main(String args[]) {
                try {
                     // Create a recognizer that supports English.
                     rec = Central.createRecognizer(
                                         new EngineModeDesc(Locale.ENGLISH));

                     // Start up the recognizer
                     rec.allocate();

                     // Load the grammar from a file, and enable it
                     FileReader reader = new FileReader("g.in");
                     RuleGrammar gram = rec.loadJSGF(reader);
                     gram.setEnabled(true);

                     // Add the listener to get results
                     rec.addResultListener(new HelloWorld());

                     // Commit the grammar
                     rec.commitChanges();

                     // Request focus and start listening
                     rec.requestFocus();
                     rec.resume();
                } catch (Exception e) {
                     e.printStackTrace();
                }
           }
      }
      the problem was in the line:
      
      rec.deallocate();
      and the error  :
      * unreported exception javax.speech.EngineException; must be caught or declared to be thrown *
      please help me .... javadev.ah@gmail.com                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
        • 1. Re: problem with jsapi code, please help me
          843802
          i tried to make rec.deallocate(); as a note then

          i had a differen problem about rec.allocate();

          the problem is:

          java.lang.NullPointerException

          at testapi.HelloWorld.main(HelloWorld.java:38)

          38: the line of rec.allocate();
          • 2. Re: problem with jsapi code, please help me
            793415
            Welcome to the Sun forums. Your code already shows how to try/catch, here is how to 'declare'.
            import javax.speech.*;
            import javax.speech.recognition.*;
            import java.io.FileReader;
            import java.util.Locale;
             
             
            public class HelloWorld extends ResultAdapter {
                 static Recognizer rec;
             
             
             
                 // Receives RESULT_ACCEPTED event: print it, clean up, exit
                 public void resultAccepted(ResultEvent e) 
                           throws EngineException  // declare the exception!
                   {
                      Result r = (Result)(e.getSource());
                      ResultToken tokens[] = r.getBestTokens();
             
                      for (int i = 0; i < tokens.length; i++)
                           System.out.print(tokens.getSpokenText() + " ");
                      System.out.println();

                      // Deallocate the recognizer and exit
                      rec.deallocate();
                      System.exit(0);
                 }

                 public static void main(String args[]) {
                      try {
                           // Create a recognizer that supports English.
                           rec = Central.createRecognizer(
                                               new EngineModeDesc(Locale.ENGLISH));

                           // Start up the recognizer
                           rec.allocate();

                           // Load the grammar from a file, and enable it
                           FileReader reader = new FileReader("g.in");
                           RuleGrammar gram = rec.loadJSGF(reader);
                           gram.setEnabled(true);

                           // Add the listener to get results
                           rec.addResultListener(new HelloWorld());

                           // Commit the grammar
                           rec.commitChanges();

                           // Request focus and start listening
                           rec.requestFocus();
                           rec.resume();
                      } catch (Exception e) {
                           e.printStackTrace();
                      }
                 }
            }
            Edit1:
            Note that unless resultAccepted(ResultEvent) is being called in the last line of the main(), it should not call System.exit(0).
            
            Edited by: AndrewThompson64 on Jul 13, 2009 1:26 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
            • 3. Re: problem with jsapi code, please help me
              843802
              hello,

              after i add the exception it gave me error saying:

              overwride method does not have exception

              but the main problem from:
              rec.allocate();
              it gave me problem saying:
              null pointer exception

              can any body help me,
              some one told me something abou fix pathes... is that right and how to do it

              Edited by: javadev.ah on Jul 12, 2009 10:24 AM
              • 4. Re: problem with jsapi code, please help me
                793415
                I did not notice that it extended something else, and did not realise that that method was overridden. But anyway, in that event, use a try/catch, like the code in main().
                • 5. Re: problem with jsapi code, please help me
                  843802
                  thank you about the exception thing I solved as exception on block.... thanks for your help

                  there is other problem about (( nullpointerexception ))
                  I will post a new question about this
                  hope you help if you know abswer

                  thanks for your help
                  • 6. Re: problem with jsapi code, please help me
                    843802
                    hello,

                    I have the same problem as yours! the null pointer exception ! can you help me plz!