title: Java NullpointerException In Facebook audience native ads, Recyclerview
body: <p>I want to display native ads in predetermined position within  my RecyclerView, I have setup the code for showing the ads template but once I integrated the code for  showing Facebook Audience native ads the app clashes, I do not know what to after this, is there any one with a solution?</p>
  
  <pre><code>  @Override
      public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          switch (viewType) {
              case 0:
                  return new ViewHolderBoxOffice(layoutInflater.from(parent.getContext()).inflate(R.layout.custom_movie_box_office, parent, false));
              case 2:
                  return new NativeAdViewHolder(layoutInflater.from(parent.getContext()).inflate(R.layout.custom_ads_facebook, parent, false));
          }
          /*View view=null;
  
         if(viewType==10){
  
              adView= (LinearLayout) layoutInflater.inflate(R.layout.custom_ads_facebook, nativeAdContainer,  false);
             nativeAdContainer.addView(adView);
          } else {
               view = layoutInflater.inflate(R.layout.custom_movie_box_office, parent, false);
  
          }
          ViewHolderBoxOffice viewHolder = new ViewHolderBoxOffice(view);
  
          return viewHolder;*/
          return null;
      }
  
      @Override
      public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
          switch (holder.getItemViewType()) {
              case 0:
                  ViewHolderBoxOffice v_holder = (ViewHolderBoxOffice) holder;
                  Movie currentMovie = mlistMovies.get(position);
                  v_holder.movieTitle.setText(currentMovie.getTitle());
                  Date movieReleaseDate = currentMovie.getReleaseDateTheater();
                  if (movieReleaseDate != null) {
                      String formmattedDate = dateFormat.format(movieReleaseDate);
                      v_holder.movieReleaseDate.setText(formmattedDate);
                  } else {
                      v_holder.movieReleaseDate.setText(Constants.NA);
  
                  }
                  int audienceScore = currentMovie.getAudienceScore();
                  if (audienceScore == -1) {
                      v_holder.movieAudienceScore.setRating(0.0F);
                      v_holder.movieAudienceScore.setAlpha(0.5F);
                  } else {
                      v_holder.movieAudienceScore.setRating(currentMovie.getAudienceScore() / 20.0F);
                      v_holder.movieAudienceScore.setAlpha(1.0F);
                  }
                  String urlThumbnail = currentMovie.getUrlThumbnail();
                  loadImages(urlThumbnail, v_holder);
              case 2:
                  NativeAdViewHolder mHolder=(NativeAdViewHolder)holder;
                  // Setting the Text.
  
                  nativeAd.unregisterView();
                  mHolder.nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
                  mHolder.nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
                  mHolder.nativeAdTitle.setText(nativeAd.getAdTitle());
                  mHolder.nativeAdBody.setText(nativeAd.getAdBody());
  
                  // Downloading and setting the ad icon.
                  NativeAd.Image adIcon = nativeAd.getAdIcon();
                  NativeAd.downloadAndDisplayImage(adIcon, mHolder.nativeAdIcon);
  
                  // Download and setting the cover image.
                  NativeAd.Image adCoverImage = nativeAd.getAdCoverImage();
                  mHolder.nativeAdMedia.setNativeAd(nativeAd);
  
  
                  // Add adChoices icon
                  if (adChoicesView == null)
                  if(! (adView.getChildAt(0) instanceof AdChoicesView)){
                      adChoicesView = new AdChoicesView(layoutInflater.getContext(), nativeAd, true);
                      adView.addView(adChoicesView, 0);
                  }
  
                  nativeAd.registerViewForInteraction(adView);
                  default:
                      break;
          }
          if (position &gt; previousPosition) {
              comeagain.materialdesign.anim.AnimationUtils.animate(holder, true);
  
          } else {
              comeagain.materialdesign.anim.AnimationUtils.animate(holder, false);
  
          }
          previousPosition = position;
  
      }
  
     /* @Override
      public void onBindViewHolder(final ViewHolderBoxOffice holder, int position) {
  
          if (position == 10) {
  
          } else {
  
          }
  
  
          if (position &gt; previousPosition) {
              comeagain.materialdesign.anim.AnimationUtils.animate(holder, true);
  
          } else {
              comeagain.materialdesign.anim.AnimationUtils.animate(holder, false);
  
          }
          previousPosition = position;
  
  
      }*/
  
      private void loadImages(String urlThumbnail, final ViewHolderBoxOffice holder) {
          if (urlThumbnail != null) {
              imageLoader.get(urlThumbnail, new ImageLoader.ImageListener() {
                  @Override
                  public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                      holder.movieThumbnail.setImageBitmap(response.getBitmap());
                  }
  
                  @Override
                  public void onErrorResponse(VolleyError error) {
  
                  }
              });
          }
      }
  
  
      @Override
      public int getItemCount() {
          return mlistMovies.size();
      }
  
      @Override
      public void onError(Ad ad, AdError adError) {
  
      }
  
      @Override
      public void onAdLoaded(Ad ad) {
  
          View adView = NativeAdView.render(layoutInflater.getContext(), nativeAd, NativeAdView.Type.HEIGHT_100);
      }
  
      @Override
      public void onAdClicked(Ad ad) {
  
      }
      @Override
      public int getItemViewType(int position) {
          int viewType = 0;
          if (position % 10 == 0) viewType = 2;
          return viewType;
      }
  
      @Override
      public void onAdsLoaded() {
  
      }
  
      @Override
      public void onAdError(AdError adError) {
  
      }
  </code></pre>
  
  <p>Here is the error</p>
  
  <pre><code>01-30 20:36:10.721 21295-21295/comeagain.materialdesign E/AndroidRuntime: FATAL EXCEPTION: main
  
  
  Process: comeagain.materialdesign, PID: 21295
                                                                        java.lang.NullPointerException
                                                                            at comeagain.materialdesign.adapters.AdapterBoxOffice.onBindViewHolder(AdapterBoxOffice.java:165)
                                                                            at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)
  </code></pre>
  
  <p>I am getting error in the line </p>
  
  <pre><code> if(! (adView.getChildAt(0) instanceof AdChoicesView)){
  </code></pre>
  
  <p>Changed to this but still getting NullPointerException</p>
  
  <pre><code>   if(! (mHolder.adview.getChildAt(0) instanceof AdChoicesView)){
                     AdChoicesView adChoicesView = new AdChoicesView(layoutInflater.getContext(), nativeAd, true);
                      mHolder.adview.addView(adChoicesView, 0);
                  }
  
                  nativeAd.registerViewForInteraction(mHolder.adview);
                  default:
                      break;
          }
          if (position &gt; previousPosition) {
              comeagain.materialdesign.anim.AnimationUtils.animate(holder, true);
  
          } else {
              comeagain.materialdesign.anim.AnimationUtils.animate(holder, false);
  
          }
          previousPosition = position;
  
      }
  
     /* @Override
      public void onBindViewHolder(final ViewHolderBoxOffice holder, int position) {
  
          if (position == 10) {
  
          } else {
  
          }
  
  
          if (position &gt; previousPosition) {
              comeagain.materialdesign.anim.AnimationUtils.animate(holder, true);
  
          } else {
              comeagain.materialdesign.anim.AnimationUtils.animate(holder, false);
  
          }
          previousPosition = position;
  
  
      }*/
  
      private void loadImages(String urlThumbnail, final ViewHolderBoxOffice holder) {
          if (urlThumbnail != null) {
              imageLoader.get(urlThumbnail, new ImageLoader.ImageListener() {
                  @Override
                  public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                      holder.movieThumbnail.setImageBitmap(response.getBitmap());
                  }
  
                  @Override
                  public void onErrorResponse(VolleyError error) {
  
                  }
              });
          }
      }
  
  
      @Override
      public int getItemCount() {
          return mlistMovies.size();
      }
  
      @Override
      public void onError(Ad ad, AdError adError) {
  
      }
  
      @Override
      public void onAdLoaded(Ad ad) {
  
  
      }
  
      @Override
      public void onAdClicked(Ad ad) {
          L.m("Ads is loaded, Jeez man that is good");
          System.out.println("Loaded in fragment");
          nativeAd = manager.nextNativeAd();
  
          nativeAd.setAdListener(this);
  
  
  
      }
      @Override
      public int getItemViewType(int position) {
          int viewType = 0;
          if (position % 10 == 0) viewType = 2;
          return viewType;
      }
  
      @Override
      public void onAdsLoaded() {
          L.m("Ads is loaded, Jeez man that is good");
          System.out.println("Loaded in fragment");
          nativeAd = manager.nextNativeAd();
  
          nativeAd.setAdListener(this);
  
  
      }
  
      @Override
      public void onAdError(AdError adError) {
  
      }
  
      class NativeAdViewHolder extends RecyclerView.ViewHolder {
          private ImageView nativeAdIcon;
          private TextView nativeAdTitle;
          private TextView nativeAdBody;
          private MediaView nativeAdMedia;
          private TextView nativeAdSocialContext;
          private Button nativeAdCallToAction;
          private LinearLayout adview;
  
  
          public NativeAdViewHolder(View itemView) {
              super(itemView);
  
  // Create native UI using the ad metadata.
              nativeAdIcon = (ImageView) itemView.findViewById(R.id.native_ad_icon);
              nativeAdTitle = (TextView) itemView.findViewById(R.id.native_ad_title);
              nativeAdBody = (TextView) itemView.findViewById(R.id.native_ad_body);
              nativeAdMedia = (MediaView) itemView.findViewById(R.id.native_ad_media);
              nativeAdSocialContext = (TextView) itemView.findViewById(R.id.native_ad_social_context);
              nativeAdCallToAction = (Button) itemView.findViewById(R.id.native_ad_call_to_action);
              adview=(LinearLayout) itemView.findViewById(R.id.ads_unit);
  
  
          }
      }
  </code></pre>
  
  <p>Here is the error</p>
  
  <pre><code>01-30 23:13:06.031 14318-14318/comeagain.materialdesign E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: comeagain.materialdesign, PID: 14318
                                                                        java.lang.NullPointerException
                                                                            at com.facebook.ads.AdChoicesView.a(Unknown Source)
                                                                            at com.facebook.ads.AdChoicesView.&lt;init&gt;(Unknown Source)
                                                                            at comeagain.materialdesign.adapters.AdapterBoxOffice.onBindViewHolder(AdapterBoxOffice.java:171)
  </code></pre>
  
  <p>Error occurs at this point</p>
  
  <pre><code> AdChoicesView adChoicesView = new AdChoicesView(layoutInflater.getContext(), nativeAd, true);
  </code></pre>