1

I am passing some data to the webservice but it is showing nullpointerexception inside the adapter like these

java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
                      at info.sherpify.pavithracs.sherpifys.adapter.RecyclerViewAdapterSearch.getItemCount(RecyclerViewAdapterSearch.java:85)

how to reslove these

this is my adapter class

package info.sherpify.pavithracs.sherpifys.adapter;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.List;
import info.sherpify.pavithracs.sherpifys.R;
import info.sherpify.pavithracs.sherpifys.model.Categorieslist;
public class RecyclerViewAdapterSearch extends RecyclerView.Adapter<RecyclerViewAdapterSearch.ViewHolder> {
    private List<Categorieslist> items;
    private OnItemClickListener onItemClickListener;
    public RecyclerViewAdapterSearch(List<Categorieslist> items) {
        this.items = items;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_home_search, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        Categorieslist item = items.get(position);

        try {


            holder.textmentordesignation.setText(item.getFieldUserDesig());
            holder.textyearofexperience.setText(item.getFieldUserExp()+" Years");
            holder.textmentorname.setText(item.getFieldUserName());
            holder.textcurrentcompany.setText("Current:"+item.getFieldUserCurrentCompany());
            holder.textpreviouscompany.setText("Previous:"+item.getFieldUserPreviousCompany());

            Log.i("Field--",""+item.getFieldUserDesig());
            Log.i("Field--",""+item.getFieldUserName());
            Log.i("Field--",""+item.getFieldUserExp());



           // holder.image.setImageBitmap(null);
            //Picasso.with(holder.image.getContext()).load(item.getCatLargeLogo()).into(holder.image);

           holder.itemView.setTag(item);
            holder.relativementor.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    onItemClickListener.onItemClick(v,items.get(position).getFieldUserId(),items.get(position).getFieldUserName(),items.get(position).getFieldUserDesig(),items.get(position).getFieldUserSummary());
                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }


    }

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


    protected static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView textmentordesignation,textyearofexperience,textmentorname,textcurrentcompany,textpreviouscompany;
        public ImageView image;
        RelativeLayout relativementor;

        public ViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.image);
            textmentordesignation = (TextView) itemView.findViewById(R.id.textmentordesignation);
            textyearofexperience = (TextView) itemView.findViewById(R.id.textyearsofexperience);
            textmentorname = (TextView) itemView.findViewById(R.id.textmentorname);
            textcurrentcompany = (TextView) itemView.findViewById(R.id.textcurrentcompany);
            textpreviouscompany = (TextView) itemView.findViewById(R.id.textpreviouscompany);
            relativementor = (RelativeLayout) itemView.findViewById(R.id.relativementor);

        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, String viewModel, String cName, String uId, String catId);
    }

}

this is my requestserver class where i am passing the data

 public List<Categorieslist> forHomeCategoryListALL(final Context cntxt, final String companyId,final String skillsId,final String industryId,final String mentor) {

        mResponseFromServerInterface.showProgressBar();

        final String homeurl = NetworkUtils.GET_HOME_CATEGORIES_MENTOR_FILTERING;

        final RequestQueue requestQueue = VolleySingleton.getInstance().getRequestQueue();
        System.out.println("Skills id request:" + skillsId);
        System.out.println("Industry id:" + industryId);
        System.out.println("Company Id :" + companyId);
        System.out.println("Mentor Id :" + mentor);



        StringRequest requestLogin = new StringRequest(Request.Method.POST, homeurl, new Response.Listener<String>() {
            @Override
            public void onResponse(final String response) {

                Log.i("Response Mentors-->",""+response);
                Log.i("Home URL-->",""+homeurl);



                try {
                    Type listType = new TypeToken<List<Categorieslist>>() {
                    }.getType();

                    mResponseFromServerInterface.hideProgressBar();
                    HomeScreenInstance.getSherpaInstance().homeCategoryMentorlist = (List<Categorieslist>) new Gson().fromJson(response.toString(), listType);
                    mResponseFromServerInterface.ResponseSuccess("Response Sucessfull");

                }
                catch (NullPointerException e)
                {
                    e.printStackTrace();
                }

            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                mResponseFromServerInterface.hideProgressBar();
                mResponseFromServerInterface.ResponseFailure("" + error.toString());
                Log.i("Exception",""+error.toString());
                error.printStackTrace();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put(Constants.MENTOR_ID, mentor);
                params.put(Constants.COMPANY_ID, companyId);
                params.put(Constants.SKILLS_ID, skillsId);
                params.put(Constants.INDUSTRY_ID, industryId);
                Log.i("Params",""+params);
                return params;
            }
        };
        requestQueue.add(requestLogin);
        return HomeScreenInstance.getSherpaInstance().homeCategoryMentorlist;
    }

this is the ouput i am getting

I/Designation Id ::  
I/SkillId  after--: null
I/IndustryId--: null
I/CompanyId--: null
I/Mentor Id ::  srini
I/Designation Id :: null
I/Params: {name= srini, skillsId=null, companyId=null, industryId=null}
I/Response Mentors-->: null
I/Home URL-->: http://192.168.10.6/sherpify/mentorsfiltering.php
I/ResponseSuccess(): Response Sucessfull
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: info.sherpify.pavithracs.sherpifys, PID: 12090
                  java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
                      at info.sherpify.pavithracs.sherpifys.adapter.RecyclerViewAdapterSearch.getItemCount(RecyclerViewAdapterSearch.java:85)
                      at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3289)
                      at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3186)
                      at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3632)
                      at android.view.View.layout(View.java:15689)
                      at android.view.ViewGroup.layout(ViewGroup.java:5040)
                      at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:636)
                      at android.view.View.layout(View.java:15689)
                      at android.view.ViewGroup.layout(ViewGroup.java:5040)
                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                      at android.view.View.layout(View.java:15689)
                      at android.view.ViewGroup.layout(ViewGroup.java:5040)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                      at android.view.View.layout(View.java:15689)
                      at android.view.ViewGroup.layout(ViewGroup.java:5040)
                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                      at android.view.View.layout(View.java:15689)
                      at android.view.ViewGroup.layout(ViewGroup.java:5040)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                      at android.view.View.layout(View.java:15689)
                      at android.view.ViewGroup.layout(ViewGroup.java:5040)
                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                      at android.view.View.layout(View.java:15689)
                      at android.view.ViewGroup.layout(ViewGroup.java:5040)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                      at android.view.View.layout(View.java:15689)
                      at android.view.ViewGroup.layout(ViewGroup.java:5040)
                      at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2116)
                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1873)
                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1084)
                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5990)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:580)
                      at android.view.Choreographer.doFrame(Choreographer.java:550)
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5343)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
pavithra
  • 105
  • 1
  • 8
  • which line is `RecyclerViewAdapterSearch.java:85` ??????? – Maveňツ Feb 07 '17 at 06:00
  • @Override public int getItemCount() { return items.size(); } – pavithra Feb 07 '17 at 06:14
  • items.size(); showing error – pavithra Feb 07 '17 at 06:14
  • Any one please suggest me where i did mistake i struck here i cannot resolve these problem – pavithra Feb 07 '17 at 06:19
  • the problem is with the mentor Id. i am giving the value as "srini" .from edittext i am fetching the value.Like this eg: String name=edtname.getText().toString(); here only i did mistake i need to fetch the data like these eg: String name=edtname.getText().toString().trim(); and one more thing as per your suggestion sir i need to check the condition if the response is null or not – pavithra Feb 09 '17 at 10:05
  • You can upvote and mark my answer as accepted to close this question. – Abhishek Jain Feb 12 '17 at 10:00

2 Answers2

0

Your HomeScreenInstance.getSherpaInstance().homeCategoryMentorlist is returning null. That is because the response you are getting is also null.

Your logcat says this : I/Response Mentors-->: null

First make sure you are getting a valid json response and then try to proceed. Also before instantiating RecyclerViewAdapterSearch do a null check to check if the list is null and only call RecyclerViewAdapterSearch() if it is not null.

That way you won't get java.lang.NullPointerException.

Abhishek Jain
  • 3,562
  • 2
  • 26
  • 44
0
 public List<Categorieslist> forHomeCategoryListALL(final Context cntxt, final String companyId,final String skillsId,final String industryId,final String name,final String designation,final String yoe) {

        mResponseFromServerInterface.showProgressBar();

        final String homeurl = NetworkUtils.GET_HOME_CATEGORIES_MENTOR_FILTERING;

        final RequestQueue requestQueue = VolleySingleton.getInstance().getRequestQueue();
        System.out.println("Skills id request:" + skillsId);
        System.out.println("Industry id:" + industryId);
        System.out.println("Company Id :" + companyId);
        System.out.println("Mentor Id :" + name);
        System.out.println("Designation Id :" + designation);
        System.out.println("yoe Id :" + yoe);



        StringRequest requestLogin = new StringRequest(Request.Method.POST, homeurl, new Response.Listener<String>() {
            @Override
            public void onResponse(final String response) {

                Log.i("Response Mentors-->",""+response);
                Log.i("Home URL-->",""+homeurl);




                try {
                    if((response!=null)&&(response.length()>4)) {
                        Type listType = new TypeToken<List<Categorieslist>>() {
                        }.getType();

                        Log.e("Response Successfull", "" + response);
                        mResponseFromServerInterface.hideProgressBar();
                        HomeScreenInstance.getSherpaInstance().homeCategoryMentorlist = (List<Categorieslist>) new Gson().fromJson(response.toString(), listType);
                        mResponseFromServerInterface.ResponseSuccess("Response Sucessfull");
                    }
                else {
                    Log.e("Response Failure", "" + response);
                    mResponseFromServerInterface.hideProgressBar();
                    BasicUtils.toast("NO RECORDS FOUND");
                }
            } catch (NullPointerException exception) {
                exception.printStackTrace();
            }
        }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                mResponseFromServerInterface.hideProgressBar();
                mResponseFromServerInterface.ResponseFailure("" + error.toString());
                Log.i("Exception",""+error.toString());
                error.printStackTrace();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put(Constants.MENTOR_ID,name);
                params.put(Constants.COMPANY_ID, companyId);
                params.put(Constants.SKILLS_ID, skillsId);
                params.put(Constants.INDUSTRY_ID, industryId);
                params.put(Constants.DESIGNATION_ID, designation);
                params.put(Constants.YEAR_OF_EXPER_ID, yoe);
                Log.i("Params",""+params);
                return params;
            }
        };
        requestQueue.add(requestLogin);
        return HomeScreenInstance.getSherpaInstance().homeCategoryMentorlist;
    }
pavithra
  • 105
  • 1
  • 8