title: java.util.List com.twitter.sdk.android.core.models.VideoInfo.variants' on a null object reference
body: <p>I'm trying to build a twitter image, video, and gif downloader app on Android Studio.
  *I'm able to download video and gif files but I'm unable to download images. It gives me an error when I put Twitter image URL in Edittext:</p>
  <blockquote>
  <p>java.lang.NullPointerException: Attempt to read from field
  'java.util.List
  com.twitter.sdk.android.core.models.VideoInfo.variants' on a null
  object reference</p>
  </blockquote>
  <p>*The error in code : <code>url = result.data.extendedEntities.media.get(0).videoInfo.variants.get(i).url;</code> How can I achieve to download images from Twitter URL?</p>
  <pre><code>import android.Manifest;
  import android.annotation.SuppressLint;
  import android.app.ProgressDialog;
  import android.content.ClipboardManager;
  import android.content.Context;
  import android.content.pm.PackageManager;
  import android.os.Build;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.MotionEvent;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.core.app.ActivityCompat;
  
  import com.esafirm.rxdownloader.RxDownloader;
  import com.shid.twittervideo.R;
  import com.shid.twittervideo.util.Constant;
  import com.twitter.sdk.android.core.Callback;
  import com.twitter.sdk.android.core.DefaultLogger;
  import com.twitter.sdk.android.core.Result;
  import com.twitter.sdk.android.core.Twitter;
  import com.twitter.sdk.android.core.TwitterApiClient;
  import com.twitter.sdk.android.core.TwitterAuthConfig;
  import com.twitter.sdk.android.core.TwitterConfig;
  import com.twitter.sdk.android.core.TwitterCore;
  import com.twitter.sdk.android.core.TwitterException;
  import com.twitter.sdk.android.core.models.Tweet;
  import com.twitter.sdk.android.core.services.StatusesService;
  
  import io.reactivex.Observer;
  import io.reactivex.disposables.Disposable;
  import retrofit2.Call;
  
  
  public class MainActivity extends AppCompatActivity {
      private Button btn_download, btn_paste;
      private ProgressDialog progressDialog;
      private TextView txt_url;
      private ClipboardManager clipboardManager;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          setUI();
          btnClick();
          checkPermission();
          setTwitterConfig();
  
      }
  
      public void setTwitterConfig() {
  
          TwitterConfig config = new TwitterConfig.Builder(this)
                  .logger(new DefaultLogger(Log.DEBUG))
                  .twitterAuthConfig(new TwitterAuthConfig(Constant.TWITTER_KEY, Constant.TWITTER_SECRET))
                  .debug(true)
                  .build();
          Twitter.initialize(config);
      }
  
      @SuppressLint(&quot;ClickableViewAccessibility&quot;)
      public void setUI() {
          btn_download = findViewById(R.id.btn_download);
          txt_url = findViewById(R.id.txt_tweet_url);
          btn_paste = findViewById(R.id.btn_paste);
  
          progressDialog = new ProgressDialog(this);
          progressDialog.setMessage(&quot;Fetching video....&quot;);
          progressDialog.setCancelable(false);
          progressDialog.setIndeterminate(true);
  
          txt_url.setOnTouchListener(new View.OnTouchListener() {
              @Override
              public boolean onTouch(View v, MotionEvent event) {
                  final int DRAWABLE_LEFT = 0;
                  final int DRAWABLE_TOP = 1;
                  final int DRAWABLE_RIGHT = 2;
                  final int DRAWABLE_BOTTOM = 3;
  
                  if (event.getAction() == MotionEvent.ACTION_UP) {
                      if (event.getRawX() &gt;= (txt_url.getRight() - txt_url.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                          // your action here
                          txt_url.setText(&quot;&quot;);
  
                          return true;
                      }
                  }
                  return false;
              }
          });
      }
  
      public void btnClick() {
          btn_paste.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                  try {
                      CharSequence textToPaste = clipboardManager.getPrimaryClip().getItemAt(0).getText();
                      txt_url.setText(textToPaste);
                  } catch (Exception e) {
                      return;
                  }
              }
          });
  
          btn_download.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  String fname;
  
                  //Check if the tweet url field has text containing twitter.com/...
                  if (txt_url.getText().length() &gt; 0 &amp;&amp; txt_url.getText().toString().contains(&quot;twitter.com/&quot;)) {
  
                      Long id = getTweetId(txt_url.getText().toString());
                      fname = String.valueOf(id);
  
                      //Call method to get tweet
                      if (id != null) {
                          getTweet(id, fname);
                      } else {
                          alertNoUrl();
                      }
                  } else{
                      alertNoUrl();
                  }
              }
          });
      }
  
      public void checkPermission() {
          //If we don't have permission we prompt the user
          if (!storageAllowed()) {
  
              ActivityCompat.requestPermissions(this, Constant.PERMISSION_STRORAGE, Constant.REQUEST_EXTERNAL_STORAGE);
          }
      }
  
      private void getTweet(Long id, String fname) {
          progressDialog.show();
  
          TwitterApiClient twitterApiClient = TwitterCore.getInstance().getApiClient();
          StatusesService statusesService = twitterApiClient.getStatusesService();
          Call&lt;Tweet&gt; tweetCall = statusesService.show(id, null, null, null);
          tweetCall.enqueue(new Callback&lt;Tweet&gt;() {
              @Override
              public void success(Result&lt;Tweet&gt; result) {
  
                  Log.d(&quot;MartianDeveloper&quot;, &quot;&quot;+result.data.extendedEntities.media.get(0).type);
  
                  //Check if media is present
                  if (result.data.extendedEntities == null &amp;&amp; result.data.entities.media == null) {
                      alertNoMedia();
                  } else if (result.data.extendedEntities != null) {
                      if (!(result.data.extendedEntities.media.get(0).type).equals(&quot;video&quot;) &amp;&amp;
                              !(result.data.extendedEntities.media.get(0).type).equals(&quot;animated_gif&quot;) &amp;&amp; !(result.data.extendedEntities.media.get(0).type).equals(&quot;photo&quot;)) {
                          alertNoVideo();
                      } else {
                          String filename = fname;
                          String url;
  
                          //Set filename to gif or mp4
                          if ((result.data.extendedEntities.media.get(0).type).equals(&quot;video&quot;) ||
                                  (result.data.extendedEntities.media.get(0).type).equals(&quot;animated_gif&quot;)) {
                              filename = filename + &quot;.mp4&quot;;
                              Log.d(&quot;TAG&quot;, &quot;filenme for video is &quot; + filename);
  
                          }else if ((result.data.extendedEntities.media.get(0).type).equals(&quot;photo&quot;)){
                              filename = filename + &quot;.jpg&quot;;
                          }
  
                          int i = 0;
                          url = result.data.extendedEntities.media.get(0).videoInfo.variants.get(i).url;
  
                          Log.d(&quot;TAG&quot;, &quot;url is &quot; + url);
  
                          while (!url.contains(&quot;.mp4&quot;)) {
                              try {
                                  if (result.data.extendedEntities.media.get(0).videoInfo.variants.get(i) != null) {
                                      url = result.data.extendedEntities.media.get(0).videoInfo.variants.get(i).url;
                                      Log.d(&quot;TAG&quot;, &quot;url2 is &quot; + url);
                                      i += 1;
                                  }
                              } catch (IndexOutOfBoundsException e) {
                                  downloadVideo(url, filename);
                              }
                          }
  
                          downloadVideo(url, filename);
                      }
                  }
  
              }
  
  
              @Override
              public void failure(TwitterException exception) {
                  Toast.makeText(MainActivity.this, &quot;Request failed, check your Internet connection&quot;, Toast.LENGTH_SHORT).show();
              }
          });
      }
  
  
      private void downloadVideo(String url, String filename) {
  
          //Check if External Storage permission js allowed
          if (!storageAllowed()) {
              // We don't have permission so prompt the user
              ActivityCompat.requestPermissions(this, Constant.PERMISSION_STRORAGE, Constant.REQUEST_EXTERNAL_STORAGE);
              progressDialog.hide();
              Toast.makeText(this, &quot;Kindly grant the request and try again&quot;, Toast.LENGTH_SHORT).show();
          } else {
              RxDownloader rxDownloader = new RxDownloader(MainActivity.this);
              rxDownloader.download(url, filename, &quot;video/*&quot;, true)
                      .subscribe(new Observer&lt;String&gt;() {
                          @Override
                          public void onSubscribe(Disposable d) {
  
                          }
  
                          @Override
                          public void onNext(String s) {
  
                          }
  
                          @Override
                          public void onError(Throwable e) {
                              Toast.makeText(MainActivity.this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                          }
  
                          @Override
                          public void onComplete() {
                              Toast.makeText(MainActivity.this, &quot;Download Complete&quot;, Toast.LENGTH_SHORT).show();
                          }
                      });
  
              progressDialog.hide();
              Toast.makeText(this, &quot;Download Started: Check Notification&quot;, Toast.LENGTH_LONG).show();
  
  
          }
      }
  
  
      private void alertNoVideo() {
          progressDialog.hide();
          Toast.makeText(this, &quot;URL entered do not contain any video or gif&quot;, Toast.LENGTH_SHORT).show();
      }
  
      private void alertNoMedia() {
          progressDialog.hide();
          Toast.makeText(this, &quot;The link entered do not contain any media&quot;, Toast.LENGTH_SHORT).show();
      }
  
  
      private Long getTweetId(String s) {
          Log.d(&quot;TAG&quot;, &quot;link is :&quot; + s);
  
          try {
              String[] split = s.split(&quot;\\/&quot;);
              String id = split[5].split(&quot;\\?&quot;)[0];
              return Long.parseLong(id);
          } catch (Exception e) {
              Log.d(&quot;TAG&quot;, &quot;getTweetId: &quot; + e.getLocalizedMessage());
              alertNoUrl();
              return null;
          }
      }
  
      private void alertNoUrl() {
  
          Toast.makeText(this,getResources().getString(R.string.toast_url) , Toast.LENGTH_LONG).show();
      }
  
  
      //Method that checks the permission depending on the version of the phone
      private boolean storageAllowed() {
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
  
              int permission = ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
  
              return permission == PackageManager.PERMISSION_GRANTED;
          }
  
          return true;
  
      }
  }
  </code></pre>