0

i am trying to call Discounted price and Discounted value from json file.

Json Content is.

[  
   {  
      "Menu_ID":"63",

      "Menu_name":"Who knows that he is hero? whole of product discre",
      "Price":"1800",
      "Discounted_Price":"180",
      "Discounted_Value":"13",
      "Menu_image":"upload\/images\/9489-2018-06-11.png"
   },
   {  
      "Menu_ID":"62",
      "Menu_name":"Who knows that he is hero?",
      "Price":"190",
      "Discounted_Price":"90",
      "Discounted_Value":"9",
      "Menu_image":"upload\/images\/0459-2018-06-10.png"
   }
]

I am using recyclerview to show all the content in my app.

Fragment Where i implement recyclerview

ArrayList<Double> Discounted_Price = new ArrayList<Double>();
ArrayList<String> Discounted_Value = new ArrayList<String>();

public void PrsJsonRea(JSONArray array){

    for(int i = 0; i<array.length(); i++) {

        SecondPojoListData getJsonRecAdap = new SecondPojoListData();

        JSONObject json = null;
        try {

            json = array.getJSONObject(i);

            getJsonRecAdap.setDiscPrice3(json.getString("Discounted_Price"));
            getJsonRecAdap.setDiscValue3(json.getString("Discounted_Value"));

            Menu_ID3.add(json.getLong("Menu_ID"));

            // Adding image title name in array to display on RecyclerView click event.
            Discounted_Value5.add(json.getString("Discounted_Value"));
            Discounted_Price5.add(Double.valueOf(formatData.format(json.getDouble("Discounted_Price"))));

        } catch (JSONException e) {

            e.printStackTrace();
        }
        recDataAdap.add(getJsonRecAdap);
    }

    madap2 = new FrontHorizontalListAdapter(recDataAdap,getContext());
    secondrecyclerView.setAdapter(madap2);
}

Adapter class

@Override
public void onBindViewHolder(ViewHolder Viewholder, int position) {

    SecondPojoListData dataAdapterOBJ =  dataAdapters.get(position);

    Viewholder.discountedvalue.setText(dataAdapterOBJ.getDiscValue3());
    Viewholder.discountedprice.setText(dataAdapterOBJ.getDiscPrice3());


}

class ViewHolder extends RecyclerView.ViewHolder{

    public TextView discountedvalue, discountedprice;
    public 

    public ViewHolder(View itemView) {

        super(itemView);
        discountedprice = (TextView) itemView.findViewById(R.id.DiscountPrice);
        discountedvalue = (TextView) itemView.findViewById(R.id.DiscountValue);

        Menuprice = (TextView) itemView.findViewById(R.id.itemprice);
    }
}

xml layout

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10sp"
        android:textColor="@color/secondary_text"
        android:maxLines="1"
        android:gravity="left|center_vertical"
        android:id="@+id/DiscountValue"
        android:padding="5dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10sp"
        android:textColor="@color/secondary_text"
        android:maxLines="1"
        android:gravity="left|center_vertical"
        android:id="@+id/DiscountPrice"
        android:padding="5dp"/>

Error Message.

06-12 09:07:11.981 32633 32633 E   AndroidRuntime                               java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
06-12 09:07:11.981 32633 32633 E   AndroidRuntime                               at com.androbaron.ecommerce.adapters.FrontHorizontalListAdapter.onBindViewHolder(FrontHorizontalListAdapter.java:83)
06-12 09:07:11.981 32633 32633 E   AndroidRuntime                               at com.androbaron.ecommerce.adapters.FrontHorizontalListAdapter.onBindViewHolder(FrontHorizontalListAdapter.java)
06-12 09:07:11.981 32633 32633 E   AndroidRuntime                               at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)
06-12 09:07:11.981 32633 32633 E   AndroidRuntime                               at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5250)
06-12 09:07:11.981 32633 32633 E   AndroidRuntime                               at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4487)
06-12 09:07:11.981 32633 32633 E   AndroidRuntime                               at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
06-12 09:07:11.981 32633 32633 E   AndroidRuntime                               at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)

Everything else working perfectly accept Discounted Price and Discounted Value

anyone help me to fix this error?

  • You should make POJO of your json response. you use can easily get your json array with your list of object. and it is easy for you to understand. – Mayur Patel Jun 12 '18 at 04:17

4 Answers4

2

Your error is a NullPointerException because one of the TextViews referenced in your ViewHolder is null. Take a look at the layout you are inflating when you create your ViewHolder to see why your referred ID is not in the ItemView you are binding to.

Bryan Dormaier
  • 800
  • 6
  • 10
1

i solve the problem by changing layout values.

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


    View viewTWO = LayoutInflater.from(parent.getContext()).inflate(R.layout.first_cardview, parent, false);
    ViewHolder rowTWO = new ViewHolder(viewTWO);
    return rowTWO;
}

thankyou for all.

1

Please typeCast Double to string like..

Viewholder.discountedvalue.setText(String.valueOf(dataAdapterOBJ.getDiscValue3()));
Monali
  • 173
  • 1
  • 6
0

The view you are trying to bind to Recyclerview seems to be misconfigured. Please double check that the layout you are using is inflate. It would be helpful if you could upload the full contents of the adapter class.

pistolcaffe
  • 849
  • 1
  • 8
  • 15