Search code examples
javanullpointerexceptiontext-to-speechruntimeexception

'android.content.res.Resources android.content.Context.getResources()' on a null object reference. Converting String array to speech


I'm developing an app in which I've Display strings and I've got a String Array. I want to convert String Array text to speech. But I'm having this error. I'm giving index to the String array so it can get converted it into speech on OnDone UtteranceProgressListener().

here's my code:

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
TextView textView;
Button button;
private boolean initialized;
private String[] queuedText;
private String TAG = "TTS";
private TextToSpeech tts;
int i = 0;


String[] Story = getResources().getStringArray(R.array.Storytxt);

String ajay = ("Once, a wolf was very hungry. It looked for food here and there. But it couldn't get any. At last, it found a loaf of bread and piece of meat in the hole of a tree.\n" +
        "\n" +
        "The hungry wolf squeezed into the hole. It ate all the food. It was a woodcutter's lunch. He was on his way back to the tree to have lunch. But he saw there was no food in the hole, instead, a wolf.\n" +
        "\n" +
        "On seeing the woodcutter, the wolf tried to get out of the hole. But it couldn't. Its tummy was swollen.\n" +
        "\n" +
        "The woodcutter caught the wolf and gave it nice beatings.");

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = findViewById(R.id.highlightxt);
    textView.setText(ajay);
    button = findViewById(R.id.button);

    tts = new TextToSpeech(getApplicationContext() /* context */, this /* listener */);

    tts.setOnUtteranceProgressListener(mProgressListener);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            speak(Story,i);
        }
    });
}

private void speak(String[] text, int i) {
    i = 0;

    if(!initialized){
        queuedText[i] = text[i];
        return;
    }
    queuedText = null;
    setTtsListner();
    HashMap<String , String> map = new HashMap<String, String>();
    map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"Message ID");
    tts.speak(text[i],TextToSpeech.QUEUE_ADD, map);

}

private void setTtsListner() {
}

@Override
public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
        initialized = true;
        tts.setLanguage(Locale.ENGLISH);

        if (queuedText != null) {
            speak(queuedText,i);
        }
    }
}
private abstract class runnable implements Runnable {
}
    private UtteranceProgressListener mProgressListener = new UtteranceProgressListener() {
        @Override
        public void onStart(String utteranceId) {
        } // Do nothing

        @Override
        public void onError(String utteranceId) {
        } // Do nothing.

        @Override
        public void onDone(String utteranceId) {

            new Thread() {
                public void run() {
                    MainActivity.this.runOnUiThread(new runnable() {
                        public void run() {

                            Toast.makeText(getBaseContext(), "TTS Completed", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }.start();
            i = i+1;
            speak(Story, i);
        }
    };

}

I'm having this error.

    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gujja.ajay.texthight/com.gujja.ajay.texthight.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

I don't know why I'm getting this. is this bcoz of the String array?

<string-array name="Storytxt">

    <item>Once, a wolf was very hungry. It looked for food here and there. But it couldn\'t get any. At last it found a loaf of bread and piece of meat in the hole of a tree.</item>
    <item>The hungry wolf squeezed into the hole. It ate all the food. It was a woodcutter\'s lunch. He was on his way back to the tree to have lunch. But he saw there was no food in the hole, instead, a wolf.</item>
    <item>On seeing the woodcutter, the wolf tried to get out of the hole. But it couldn\'t. Its tummy was swollen.</item>
    <item>The woodcutter caught the wolf and gave it nice beatings.</item>

</string-array>

Solution

  • It appears you're attempting to use getResources() on a Context (which in this case is implicitly the MainActivity class, 'this')... which hasn't been initialized yet... because onCreate hasn't run yet.

    So, you probably just need to:

    Change:

    String[] Story = getResources().getStringArray(R.array.Storytxt);
    

    To:

    String[] Story;
    

    Then inside onCreate, put:

    Story = getResources().getStringArray(R.array.Storytxt);