Java – NullPointerException when using TTS in CustomListview

NullPointerException when using TTS in CustomListview… here is a solution to the problem.

NullPointerException when using TTS in CustomListview

I have a custom ListView with content and TTS options, throws a null pointer exception when using TTS, and the Listview also doesn’t show that my app is stopped. An error is displayed in Logcat.

App adapter .java

public class ApplicationAdapter extends ArrayAdapter<Application> implements
    private List<Application> items;
    private LayoutInflater inflator;
    private MainActivity activity;

private ProgressDialog dialog;
    public TextToSpeech tts;
    public ImageButton btnaudioprayer;
   public TextView text1;

ArrayAdapter<String> adapter;

public ApplicationAdapter(MainActivity context, List<Application> items){
        super(context, R.layout.activity_row, items);
        this.items = items;

inflator = LayoutInflater.from(getContext());


    public int getCount(){
        return items.size();

    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolder holder = null;

tts = new TextToSpeech(activity, ApplicationAdapter.this);

View v = convertView;
        if ( convertView == null ){ 
            convertView = inflator.inflate(R.layout.activity_row, null);
            holder = new ViewHolder();
            holder.text2 = (TextView) convertView.findViewById(;
            holder.text1 = (TextView) convertView.findViewById(;
            holder.count = (TextView) convertView.findViewById(; 
            holder.pray  = (Button) convertView.findViewById(;
            holder.chk = (CheckBox) convertView.findViewById(;
            holder.btnSpeak = (ImageButton) convertView.findViewById(;

        }else {
            holder = (ViewHolder) convertView.getTag();

holder.chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton view,
                    boolean isChecked) {
                int getPosition = (Integer) view.getTag();


holder.pray.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                int getPosition= (Integer)v.getTag();
                StringBuffer sb1 = new StringBuffer();
                sb1.append("ID :");



holder.btnSpeak.setOnClickListener(new View.OnClickListener() {

                public void onClick(View V) {



Application app = items.get(position);

return convertView;
    static class ViewHolder {
        public TextView text2;
        public TextView text1;
        public TextView count;
        public CheckBox chk;
        public Button pray;
        public ImageButton btnSpeak;
        private TextToSpeech tts;
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {

int result = tts.setLanguage(Locale.US);

if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {


} else {
            Log.e("TTS", "Initilization Failed!");


private void speakOut() {

String text = text1.getText().toString();

tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);

Here I also mentioned my logcat error. In my logcat error, it shows the error in this line
String text = text1.getText().toString(); 。 It throws a NullPointerException and also displays the log.e file. It does not run the speakout() method.

07-18 12:17:11.456: E/TTS(2314): This Language is not supported
07-18 12:17:11.456: I/TextToSpeech(2314): Connected to ComponentInfo{com.svox.pico/com.svox.pico.PicoService}
07-18 12:17:11.586: D/AndroidRuntime(2314): Shutting down VM
07-18 12:17:11.586: W/dalvikvm(2314): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
07-18 12:17:11.597: E/AndroidRuntime(2314): FATAL EXCEPTION: main
07-18 12:17:11.597: E/AndroidRuntime(2314): java.lang.NullPointerException
07-18 12:17:11.597: E/AndroidRuntime(2314):     at com.example.jsonandroid.ApplicationAdapter.speakOut(
07-18 12:17:11.597: E/AndroidRuntime(2314):     at com.example.jsonandroid.ApplicationAdapter.onInit(
07-18 12:17:11.597: E/AndroidRuntime(2314):     at android.speech.tts.TextToSpeech.dispatchOnInit(
07-18 12:17:11.597: E/AndroidRuntime(2314):     at android.speech.tts.TextToSpeech.access$1000(
07-18 12:17:11.597: E/AndroidRuntime(2314):     at android.speech.tts.TextToSpeech$Connection.onServiceConnected(
07-18 12:17:11.597: E/AndroidRuntime(2314):     at$ServiceDispatcher.doConnected(
07-18 12:17:11.597: E/AndroidRuntime(2314):     at$ServiceDispatcher$
07-18 12:17:11.597: E/AndroidRuntime(2314):     at android.os.Handler.handleCallback(
07-18 12:17:11.597: E/AndroidRuntime(2314):     at android.os.Handler.dispatchMessage(
07-18 12:17:11.597: E/AndroidRuntime(2314):     at android.os.Looper.loop(
07-18 12:17:11.597: E/AndroidRuntime(2314):     at
07-18 12:17:11.597: E/AndroidRuntime(2314):     at java.lang.reflect.Method.invokeNative(Native Method)
07-18 12:17:11.597: E/AndroidRuntime(2314):     at java.lang.reflect.Method.invoke(
07-18 12:17:11.597: E/AndroidRuntime(2314):     at$

In my client ListView, I have text and TTS buttons that must play audio for the content when clicked.


Your variable text1 is not initialized but declared, so by executing text1.getText(), you are actually trying to getText() from null.

Maybe what you want is holder.text1 (holder in ViewHolder)?

Related Problems and Solutions