tags:

views:

1052

answers:

4

Hi I want to use speech recognition in my project and I found this code but when I run it I get an error which is rec is null java.lang.NullPointerException at newpackage.HelloWorld.main(HelloWorld.java:55)

please could one of you help me in this problem or if you have other code could you send it to me. This of server code that I use:

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.speech.*;
import javax.speech.recognition.*;
import java.io.FileReader;
import java.util.Locale;


public class HelloWorld extends ResultAdapter {
    static Recognizer rec;
    // private static Object loadJSGF;

    // 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[i].getSpokenText() + " ");
    System.out.println();
        try {
            // Deallocate the recognizer and exit
            rec.deallocate();
        } catch (EngineException ex) {
            Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
        } catch (EngineStateError ex) {
            Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.exit(0);
}

public static void main(String args[]) {
 try {
          // Create a recognizer that supports English.
     rec = Central.createRecognizer(new EngineModeDesc(Locale.ENGLISH));
                if (rec != null) System.out.println(rec);
            else {
                System.out.println("rec is null");
            }

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

  // Load the grammar from a file, and enable it
  FileReader reader = new FileReader(args[0]);
  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();
      // System.out.println("the problem");
 }
}
}
+1  A: 
if (rec != null) {
    System.out.println(rec);
}
else {
    System.out.println("rec is null");   
    // <-- here's your problem.  you need to return, exit, or throw here!
}

// Start up the recognizer
rec.allocate();  // <-- This is the line that's blowing out (I assume)

You'll get a null pointer because even though you're else is handling the case when rec is null, you're program continues. You need to return or exit, or something when rec is null.

Note: Also, I reformatted your code because it's hard to read your if/else. If you're going to use curlies on one branch of your if/else, you should use curlies on both. It makes it more readible.

Edit: Oh yeah, as far as why createRecognizer is returning null, I'm afraid I don't know.

dustyburwell
Now I see the error. Should have seen that, too, but I was too confused with that indention and the two curly braces at the end.
schnaader
A: 

sorry i put the error code the correct code is /* * To change this template, choose Tools | Templates * and open the template in the editor. */

package newpackage;

/** * * @author 200401652 / import java.util.logging.Level; import java.util.logging.Logger; import javax.speech.; import javax.speech.recognition.*; import java.io.FileReader; import java.util.Locale;

public class HelloWorld extends ResultAdapter { static Recognizer rec; // private static Object loadJSGF;

// 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[i].getSpokenText() + " ");
 System.out.println();
    try {
        // Deallocate the recognizer and exit
        rec.deallocate();
    } catch (EngineException ex) {
        Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
    } catch (EngineStateError ex) {
        Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
    }
 System.exit(0);
}

public static void main(String args[]) {
 try {
  // Create a recognizer that supports English.
  rec = Central.createRecognizer(
      new EngineModeDesc(Locale.ENGLISH));
        /*if (rec != null) System.out.println(rec);
        else {
            System.out.println("rec is null");

        }*/

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

  // Load the grammar from a file, and enable it
  FileReader reader = new FileReader(args[0]);
  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();
      // System.out.println("the problem");
 }
}

}

and when i run it i get run: java.lang.NullPointerException at newpackage.HelloWorld.main(HelloWorld.java:55)

A: 

When i run this program ,i get rec is null. Can anyone please help me to get rid off this problem. is this a problem of JSAPI... please help me.........

import java.util.logging.Level; import java.util.logging.Logger; import javax.speech.; import javax.speech.recognition.; import java.io.FileReader; import java.util.Locale;

public class HelloWorld extends ResultAdapter { static Recognizer rec; // private static Object loadJSGF;

// 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[i].getSpokenText() + " ");
System.out.println();
    try {
        // Deallocate the recognizer and exit
        rec.deallocate();
    } catch (EngineException ex) {
        Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
    } catch (EngineStateError ex) {
        Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.exit(0);

}

public static void main(String args[]) { try { // Create a recognizer that supports English. rec = Central.createRecognizer(new EngineModeDesc(Locale.ENGLISH)); if (rec != null) System.out.println(rec); else { System.out.println("rec is null"); }

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

            // Load the grammar from a file, and enable it
            FileReader reader = new FileReader("test.gram");
            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();
  // System.out.println("the problem");
    }

} }

Kundan
A: 

Hi all, I have the same issue. When I run rec is null. Please could you help me? Have you resolved that issue?

Thx a lot, rosario