0

I currently have

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

private List<SectionOrRow> mData;

public MyRecycler(List<SectionOrRow> data) {
    mData = data;
}

...

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

...

}

and need to pass a list of objects using

SectionOrRow.createRow("row x");

or

SectionOrRow.createSection("section x");

however I do not know where or how to construct this list. Adding it so that the code becomes

public Adapter(List<SectionOrRow> data) {
    SectionOrRow.createRow("row 1");
    SectionOrRow.createSection("section 1");
    mData = data;
}

gives the following error:

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

Inferring, this sounds like my mData has size is null, as the error occurs on the line that returns the mData size; so obviously adding it inside that class is incorrect. What is the correct way to construct the list?

Edit 1: SectionOrRow class:

public class SectionOrRow {

private String row;
private String section;
private boolean isRow;

public static SectionOrRow createRow(String row) {
    SectionOrRow ret = new SectionOrRow();
    ret.row = row;
    ret.isRow = true;
    return ret;
}

public static SectionOrRow createSection(String section) {
    SectionOrRow ret = new SectionOrRow();
    ret.section = section;
    ret.isRow = false;
    return ret;
}

public String getRow() {
    return row;
}

public String getSection() {
    return section;
}

public boolean isRow() {
    return isRow;
}

}

Talisman
  • 388
  • 4
  • 19
  • add the code of `SectionOrRow` – MJM Jan 20 '18 at 07:40
  • Added! @jignesh – Talisman Jan 20 '18 at 07:43
  • Show the code you use `MyRecycler` . – KeLiuyue Jan 20 '18 at 07:58
  • 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) – Bharatesh Jan 20 '18 at 08:14
  • In your static methods I think your making 2 different objects that dont satisfy the properties of the SectionOrRow class. Your Instantiating a new object each time in the method body. Since its a bool you could flip it and set one of the properties to a " " empty string. ex. 'public static SectionOrRow createRow(String row) { SectionOrRow ret = new SectionOrRow(); ret.row = row; ret.isRow = true; if(ret.isRow = true) ret.section= " -";//--something to make it not null. return ret;' – Mohamoud Mohamed Jan 20 '18 at 08:40
  • @Talisman show the code from you setup `Adapter` – MJM Jan 20 '18 at 09:10
  • Make setter for each variable like sectionOrRow.setRow("any row"); same as well for other two variables and after that add this class object in your generic list you have created. – Arora Jan 20 '18 at 12:04
  • @Arora I think I'm hung up on how to actually add the class object to the list - the rest of the code is in place. – Talisman Jan 20 '18 at 19:18
  • send me your code I will do it for you – Arora Jan 22 '18 at 06:05

0 Answers0