Android ApolloCall.CallBack onResponse not fetching data

598 views Asked by At

Im using this query for fetching data:

query Products($id: ID!){
            node(id: $id) {
                    id
                    ... on Collection {
                                title
                                products(first: 250){
                                                edges{
                                                    node{
                                                    title
                                                    id
                                            variants(first: 1){
                                                edges{
                                                    node{
                                                        price
                                                        }
                                                    }
                                                }
                                            images(first: 1,maxWidth:400,maxHeight:740,scale:3){
                                                edges{
                                                    node{
                                                        src
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

In OnResponse method of ApolloCall.CallBack when i want do fetch product title it generates nullPointerException on this line " Log.i("CallBack products", String.valueOf(response.data().node().asCollection().products().edges())); " i need to display product's title,price and image in recyclerview. what is the best way to fetch them? Thanks in advance!

below is my code:

      @Override                                                                                                                 
 protected void onCreate(@Nullable Bundle savedInstanceState) {                                                            
     super.onCreate(savedInstanceState);                                                                                   
     setContentView(R.layout.activity_product);                                                                            
     String s = getIntent().getStringExtra("id");                                                                          
     Bundle extras = getIntent().getExtras();                                                                              
     if (extras != null) {                                                                                                 
         s = extras.getString("id");                                                                                       
     }                                                                                                                     
     application = (SampleApplication) getApplication();                                                                 


     content = (ViewGroup) findViewById(R.id.rl_content_holder);                                                           
     progressBar = (ProgressBar) findViewById(R.id.loading_bar);                                                           
     productRecyclerView = (RecyclerView) findViewById(R.id.rvProductList);                                                
     mProductAdapter = new ProductAdapter(this);                                                                           
     mGridLayoutManager = new GridLayoutManager(this,2);                                                                   

     fetchProducts();                                                                                                      
 }                                                                                                                         

 ApolloCall.Callback<Products.Data> mDataCallback =                                                                        
         new ApolloCall.Callback<Products.Data>() {                                                                        

             @Override                                                                                                     
             public void onResponse(@Nonnull final Response<Products.Data> response) {                                     

                     Log.i("CallBack products", String.valueOf(response.data().node().asCollection().products().edges())); 

                     runOnUiThread(new Runnable() {                                                                        
                         @Override                                                                                         
                         public void run() {                                                                               
                             Toast.makeText(ProductActivity.this, "getting products", Toast.LENGTH_SHORT).show();          
                             mProductAdapter.setProductData(response.data().node().asCollection().products().edges());     
                             productRecyclerView.setAdapter(mProductAdapter);                                              
                             productRecyclerView.setLayoutManager(mGridLayoutManager);                                     
                             progressBar.setVisibility(View.GONE);                                                         
                             content.setVisibility(View.VISIBLE);                                                          
                         }                                                                                                 
                     });                                                                                                   

             }                                                                                                             

             @Override                                                                                                     
             public void onFailure(@Nonnull ApolloException e) {                                                           
                 Log.e("Product Activity", e.getMessage(), e);                                                             
                 runOnUiThread(new Runnable() {                                                                            
                     @Override                                                                                             
                     public void run() {                                                                                   
                         progressBar.setVisibility(View.GONE);                                                             
                         Toast.makeText(ProductActivity.this, "getting products failed", Toast.LENGTH_SHORT).show();       
                     }                                                                                                     
                 });                                                                                                       
             }                                                                                                             
         };                                                                                                                
 public void fetchProducts() {                                                                                             

     mHttpClient = new OkHttpClient.Builder()                                                                              
             .addInterceptor(new HttpInterceptor())                                                                        
             .build();                                                                                                     
     Products productQuery = Products                                                                                      
             .builder().id("id")                                                                                           
             .build();                                                                                                     
     productCall = application.apolloClient().newCall(productQuery);                                                       
     productCall.enqueue(mDataCallback);                                                                                   

 }  

and here is RecyclerView adapter:

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> {

    // Store the context for easy access
    private Context mContext;

    private List<Products.Data.Edge> productData = Collections.emptyList();

    public void setProductData(List<Products.Data.Edge>productData) {
        this.productData = productData;
        this.notifyDataSetChanged();
    }

    public ProductAdapter(Context context ) {

        mContext = context;
    }

    private Context getContext() {
        return mContext;
    }



    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView productListItem;
        public ImageView productListImage;
        private View rlContainer;
        Context context;

        public ViewHolder(Context context,View itemView) {
            super(itemView);
            productListItem = (TextView) itemView.findViewById(R.id.itemProductTxt);
            productListImage = (ImageView)itemView.findViewById(R.id.itemProductImg);
            rlContainer =  itemView.findViewById(R.id.linear_layout);
            this.context = context;
        }

        public void setProductItem(final Products.Data.Edge productItem) {

            productListItem.setText(productItem.node().title());


            rlContainer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Toast.makeText(context, "list item pressed", Toast.LENGTH_SHORT).show();

                }
            });
        }
    }

    @Override
    public ProductAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        // Inflate the custom layout
        View productView = inflater.inflate(R.layout.listitem_product, parent, false);

        // Return a new holder instance
        ProductAdapter.ViewHolder viewHolder = new ProductAdapter.ViewHolder(context,productView);
        return viewHolder;
    }


    @Override
    public void onBindViewHolder(ProductAdapter.ViewHolder holder, int position) {

        final Products.Data.Edge productsEntry = this.productData.get(position);
        holder.setProductItem(productsEntry);



    @Override
    public int getItemCount() {
        return productData.size();
    }


}
1

There are 1 answers

0
Ivan Savytskyi On

Could you please provide the stack trace. And are you sure that you get back not empty response?

You get NPE probably because the requested node by id doesn't exist.