title: Set the selected language for Text to Speech in Java
body: <p>I have a list of many languages that the user can select for a text-to-speech operation. My wish is to set the language that the user has selected for tts to function. But setLanguage requires a Locale, and by that stage, I only have a String. So this is what I have tried to do, but I get a nullpointer exception:</p>
  
  <pre><code>@Override
  public void onClick(View view) {
      Languages currentLanguages = languagesList.get(getAdapterPosition());
      Snackbar snackbar;
      snackbar = Snackbar.make(view, currentLanguages.getLocaleDisplay() + " selected", Snackbar.LENGTH_INDEFINITE)
              .setAction("Next", new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      Intent intent = new Intent(v.getContext(), MainActivity.class);
                      String chosenLanguage = localeDisplay.getText().toString();
                      System.out.println(chosenLanguage);
                      intent.putExtra("language_key", chosenLanguage);
                      v.getContext().startActivity(intent);
                  }
              });
      Locale chosenLocale = localeDisplay.getTextLocale();
      chosenLocale.getLanguage();
      myTTS.setLanguage(chosenLocale);
      View snackBarView = snackbar.getView();
      snackBarView.setBackgroundColor(grey_blue);
      snackbar.show();
  }
  </code></pre>
  
  <p>I would be extremely grateful for some pointers as to why this is not working. Thanks!</p>
  
  <p>Here is the logcat:</p>
  
  <pre><code>FATAL EXCEPTION: main
     Process: com.madhatter.nat.pronounce, PID: 864
     java.lang.NullPointerException: Attempt to invoke virtual method 'int android.speech.tts.TextToSpeech.setLanguage(java.util.Locale)' on a null object reference
         at com.madhatter.nat.pronounce.LanguagesRecycler.RecyclerAdapter$MyViewHolder$1.onClick(RecyclerAdapter.java:63)
         at android.view.View.performClick(View.java:5198)
         at android.view.View$PerformClick.run(View.java:21147)
         at android.os.Handler.handleCallback(Handler.java:739)
         at android.os.Handler.dispatchMessage(Handler.java:95)
         at android.os.Looper.loop(Looper.java:148)
         at android.app.ActivityThread.main(ActivityThread.java:5417)
         at java.lang.reflect.Method.invoke(Native Method)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  </code></pre>
  
  <p>Edit: getAdapterPosition() gives me the number of the language that is clicked. How can I get to set the language of the Text-To-Speech engine based on its position in my recycler list? </p>