-4

I'm trying to display data from mysql database with recycler-view ans card-view, but i'm facing this error message:

java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference

Everything seems to work properly because I'm able to display data retrieved on (Log) in my loop, and when I'm trying to add data on my List data_list, every thing is OK, because I'm able to know the number of lines returned by my data_list

Log.e("number of lines return", String.valueOf(data_list.size()));

But when i'm trying to reach the data_list on my Custom Adapter:

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

It warns me i can't access my_data because it's null.

The part of my code which retrieves data from my database:

private void load_data_from_server(final int id) {
    //Log.e("iddddsss", String.valueOf(id));
    AsyncTask<Integer,Void,Void> task =new AsyncTask<Integer, Void, Void>(){

        @Override
        protected Void doInBackground(Integer... integers) {

            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()

                    .url("http://www.unhabitatrdcongo.org/Recycler-android-mySQL/index2.php?id="+id)
                    .build();
            try {
                Response response = client.newCall(request).execute();
                JSONArray array = new JSONArray(response.body().string());



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

                    Integer idd = object.getInt("id");
                    String des = object.getString("description").toString();
                    String im =object.getString("image").toString();

                    MyData data = new MyData(idd, des, im);

                    Log.e("errr", data.getDescription().toString());
                    data_list.add(data);


                }

        Log.e("number of lines read", String.valueOf(data_list.size()));

            } catch (IOException e) {
                e.printStackTrace();
                Log.e("erreur IO EXCEPTION", e.toString());
            } catch (JSONException e) {
                System.out.print("End of content");
                Log.e("erreur JSON",e.toString());
            }
            return null;
        }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

Generally, my_data has not been assigned yet while executing getItemCount (). This method is called in the adapter construct this.my_data = data_list;

Autumn
  • 11
  • 3
0

Most likely the List<MyData> data_listthat the class is getting is null, and then, when this.my_data = data_list; you are doing this this.my_data = null;

Try to check that.

Alejandro Rueda
  • 111
  • 1
  • 5
  • public class CustomAdapter extends RecyclerView.Adapter { private Context context; private List my_data; public CustomAdapter(Context context, List data_list) { this.context = context; this.my_data = data_list; } – BENOIT TAMBWE Dec 15 '17 at 13:59
  • Same thing, seems the list that `CustomAdapter` recieves as parameter is null. Have you tryed to debug your proyect? – Alejandro Rueda Dec 16 '17 at 00:19