-5

ImageI want to implement one code where data will come from server and show on RecyclerView item, below is my mainActivity class where I am fetching the data from server but this code is not running just because of NullPointerException. I am using Volley to fetch the data

public class MainActivity_Plan extends AppCompatActivity {
private List<Plan> planList = new ArrayList<>();
private RecyclerView recyclerView;
private PlanAdapter pAdapter;
private RequestQueue requestQueue;
String url;

// Session Manager Class
SessionManager session;
String matri_id;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.plan_activity);
    session = new SessionManager(this);
    HashMap<String, String> user = session.getUserDetails();
    matri_id = user.get(SessionManager.KEY_EMAIL);

    Log.e("matri_id+++++++++++++",matri_id);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    pAdapter = new PlanAdapter(planList);
    RecyclerView.LayoutManager mLayoutManager = new 
    LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(pAdapter);
    getInfo();
    Log.e("url____________",url);
}

 private void getInfo() {
    url = ConfigPlan.DATA_URL+matri_id;

JsonArrayRequest of volley

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    //Calling method parseData to parse the json response
                    Log.e("response---------",response.toString());
                    parseData(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //If an error occurs that means end of the list has reached
                    Toast.makeText(MainActivity_Plan.this, "No More Items Available", Toast.LENGTH_SHORT).show();
                }
            });

    requestQueue.add(jsonArrayRequest);
}

This method will parse json data

  private void parseData(JSONArray array) {
    for (int i = 0; i < array.length(); i++) {
        Plan p = new Plan();
        JSONObject json = null;
        try {
            json = array.getJSONObject(i);
            //Adding data to the superhero object
            p.setName(json.getString(ConfigPlan.KEY_PLAN_NAME));
            p.setAmount(json.getString(ConfigPlan.KEY_PLAN_AMOUNT));
            p.setDuration(json.getString(ConfigPlan.KEY_PLAN_DURATION));
            p.setContacts(json.getString(ConfigPlan.KEY_PLAN_CONTACTS));
            } catch (JSONException e) {
            e.printStackTrace();
        }
        //Adding the superhero object to the list
        planList.add(p);
    }
  }
}

Below is Adapter Class

public class PlanAdapter extends RecyclerView.Adapter<PlanAdapter.MyViewHolder> {

private List<Plan> planList;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView plan_name, plan_duration, plan_contact, plan_amount;
    public Button subscribeButton;

    public MyViewHolder(View view) {
        super(view);
        plan_name = (TextView) view.findViewById(R.id.planName);
        plan_duration = (TextView)view.findViewById(R.id.planDuration);
        plan_contact = (TextView) view.findViewById(R.id.planContacts);
        plan_amount = (TextView) view.findViewById(R.id.planAmount);
        subscribeButton = (Button) view.findViewById(R.id.subscribeButton);
    }
}

public PlanAdapter(List<Plan> planList) {
    this.planList = planList;
}

@Override
public PlanAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
        .inflate(R.layout.plan_activity_item, parent, false);
    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(PlanAdapter.MyViewHolder holder, int position) {
    Plan plan = planList.get(position);
    holder.plan_name.setText(plan.getName());
    holder.plan_amount.setText(plan.getAmount());
    holder.plan_contact.setText(plan.getContacts());
    holder.plan_duration.setText(plan.getDuration());
  }
  @Override
  public int getItemCount() {
     return 0;
  }
 }
Vikas Godiyal
  • 117
  • 3
  • 15

3 Answers3

1

I think you might not have initialized requestQueue object of a class RequestQueue (as per your shared error image).

Dhruv Patel
  • 1,529
  • 1
  • 18
  • 27
0

Please update adapter on list, after you done with parsing of code. You have to put "Adapter" Code in "OnResponse()" method. After parsing code.

Ankit Patidar
  • 2,731
  • 1
  • 14
  • 22
0
  • not have initialized requestQueue object of a class RequestQueue

  • you forgot to pAdapter.notifyDataSetChanged(); after adding data in your planList Arraylist

  • change this in your adapter class

Change in getItemCount() method

 @Override
      public int getItemCount() {
         return planList.size;
      }
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31