0

I am using the TextToSpeech API and I want to seperate some logic into another class.

In the separate class I have put the following method:

public static void sayHello() {
// Select a random hello.
int helloLength = SoundGameScore.Questions.length;
String hello = SoundGameScore.Questions[currentHelloIndex];
currentHelloIndex = (currentHelloIndex + 1) % helloLength;
mTts.speak(hello, TextToSpeech.QUEUE_FLUSH, // Drop all pending entries
                                            // in the playback queue.
        null);

I have then created a variable in the main class: static mainclass object;

Within a button in the main class I call the method through this object by using:

object.sayHello();

I am quite new to android, and I know I am doing something wrong as this gives me a process closed error in the emulator. This also shows a nullexception error in logcat. Please help me, thanks.

1 Answer 1

1

I think you are getting a NullPointerException because the reference object is null. You would need to initialise the object in order to call an instance method on it.

However since sayHello() is a static method, you do not need to create an instance of the class in order to call the method. Just use mainclass.sayHello().

Your question and code suggests to me that you do not have much experience with Java. Perhaps you should do some tutorials to brush up on your Java coding before jumping into Android development. For example, Java convention is for class names to be capitalised (MainClass) and for references to have meaningful names (i.e. not things like object).

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Dave, I have called it previously by using the Mainclass.sayHello() but this still gives me the same error. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.