0

I have been working on recyclerView but when i run my app it is not showing any data on the activity and app crashes and it give error on this method

Here is my Adaptor class:

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


private  ArrayList<info> Info=new ArrayList<>();

public Adaptor(ArrayList<info> info) {
    this.Info = info;
}


public void updateList(ArrayList<info> data) {
    Info = data;
    notifyDataSetChanged();
}

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


    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_view, parent, false);

         return new ViewHolder(itemView);


}



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

    info i= Info.get(position);
    holder.name.setText(i.getName());
    holder.city.setText(i.getCity());
    holder.city.setText(i.getUniversity());


}



public class ViewHolder extends RecyclerView.ViewHolder {

    TextView name, city, uni;


    public ViewHolder(final View v) {
        super(v);
        name = (TextView) v.findViewById(R.id.name);
        city = (TextView) v.findViewById(R.id.city);
        uni = (TextView) v.findViewById(R.id.university);

    }




}

public int getItemCount() {
    return Info.size();
}

}

Here is my activity class:

    public class Home extends AppCompatActivity implements HomeCallBack {

    public ArrayList<info> Info;
    private  Adaptor myadaptor;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;



@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    recyclerView=(RecyclerView)findViewById(R.id.recycle_view);
      Info=new ArrayList<>();

    myadaptor=new Adaptor(Info);
    recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
    LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(myadaptor);



    BackgroundHome backgroundHome=new BackgroundHome(this,this);
    backgroundHome.execute();


}

@Override
public void processData(JSONArray data)
{

    JSONObject js;

            if(data!=null)
            {
                Info=new ArrayList<>();


                try
                {
                    for(int i=0;i<data.length();i++)
                    {
                        js =data.getJSONObject(i);
                        info in=new 


  info(js.getString("Names"),js.getString("city"),
  js.getString("university"));
                        Info.add( in);

                    }


                    myadaptor=new Adaptor(Info);

                    recyclerView.setAdapter(myadaptor);

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

                myadaptor.updateList(Info);


               }

Whenever i run it, it crashes and the error occur in getItemcount() method that it is null but i have initialize it to Info.size();

Raheel Sajjad
  • 73
  • 1
  • 9
  • dont Initialise your Info arrayList in your Adapter Class.... remove this from adapter class....just write ArrayList Info; no need to initialise it. Initialising is to be done in activity. – Sahil Sep 01 '17 at 21:35
  • The single piece of information we need, to find your problem, is the stack trace. Please, don't bother including your entire program's code here. *Just show us the stacktrace*. https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it/218510#218510 – G. Blake Meike Sep 02 '17 at 02:09
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – G. Blake Meike Sep 02 '17 at 02:12

2 Answers2

0

In your processData function, you're creating an Info list, adding new items to it. That's all good.

However, after that you're creating a new adapter with that Info list, then you're setting that adapter again with the Info list.

myadaptor =new Adaptor(Info);
recyclerView.setAdapter(myadaptor);

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

myadaptor.updateList(Info);

Perhaps that's causing issues with it becoming null.

advice
  • 5,778
  • 10
  • 33
  • 60
  • he does not need these lines yes but i don't think that is the problem. If you think about it what he do .. he set new adapter object to his recycle view .. just object changing no more .. then he set the list he created in it again in this line `myadaptor.updateList(Info);` and also there is no problem with that – Mohamed Nagy Mostafa Sep 02 '17 at 01:06
0

okey .. i checked your code. For me there is no obvious reason for this exception but anyway let us fix and debug some issues.

before anything please change the name of you objects/class ...

/// you have class with name 'info'
private  ArrayList<info> Info=new ArrayList<>();
// here you set variable with the same class name !!! 
public Adaptor(ArrayList<info> info) {
  this.Info = info;
}

And test your program if the problem is still then let use test your project.

Firstly in your adapter replace this line private ArrayList<info> Info=new ArrayList<>(); to private ArrayList<info> Info= null;.Then let us debug your code ..

Firstly set these lines as a comment BackgroundHome backgroundHome=new BackgroundHome(this,this); backgroundHome.execute(); then run your program .. Mostly your program is going to run without error .. if this happened then your error in processData(JSONArray data)

Then let us check your code and modify it. You do not need these lines .

 myadaptor=new Adaptor(Info); // delete

 recyclerView.setAdapter(myadaptor);// delete

Now run your program ..

  • my motive is not to run app without crashing but also to show data on the activity.. – Raheel Sajjad Sep 02 '17 at 21:07
  • after i done all what you have tell me , the app is running but nothing is displaying anything there!! – Raheel Sajjad Sep 02 '17 at 21:12
  • okey .. null error is disappeared !!! i expected this result no problem i can tell you what is the next step but firstly i need you to do something ... set Log below `myadaptor.updateList(Info);` and in this log display the size if info list. also set log in `getItemCount()` before return and check also size of info list and tell me the result – Mohamed Nagy Mostafa Sep 03 '17 at 17:48