0

I am trying to use table layout with recyclerView but it is giving the error:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TableRow.addView(android.view.View)' on a null object reference.

There is my adapter class:

public class CompanyAdapter extends RecyclerView.Adapter<CompanyAdapter.bookViewHolder> {

ArrayList<DataClass> arrayList;
TextView deposit;
TextView purchase;
TextView date;
TableRow tableRow;
TableLayout tableLayout;

public CompanyAdapter(ArrayList<DataClass> arrayList){
    this.arrayList = arrayList;
}

@Override
public bookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    View view = 

 LayoutInflater.
from(context).inflate(R.layout.company_list_item,parent,false);
    return new bookViewHolder(view);
}

@Override
public void onBindViewHolder(bookViewHolder holder, int position) {
    DataClass dataClass = arrayList.get(position);
    holder.bind(dataClass);
    //bookNameTV.setText(dataClass.getBook());
    //authorNameTV.setText(dataClass.getAuthor());


}

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

public class bookViewHolder extends RecyclerView.ViewHolder{



    public bookViewHolder(View itemView) {
        super(itemView);
        deposit = (TextView) itemView.findViewById(R.id.deposit);
        purchase = (TextView) itemView.findViewById(R.id.purchase);
        date = (TextView) itemView.findViewById(R.id.date);
        tableLayout = (TableLayout) 
itemView.findViewById(R.id.maintablelayout);

    }

    public void bind(DataClass dataClass){
        deposit.setText(Integer.toString(dataClass.getAmount()));
        purchase.setText(Integer.toString(dataClass.getAmount()));
        date.setText(dataClass.date);
        tableRow.addView(deposit);
        tableRow.addView(purchase);
        tableRow.addView(date);
        tableLayout.addView(tableRow, new TableLayout.LayoutParams(
                TableLayout.LayoutParams.FILL_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT));// giving error

    }
}
}

One more question, Is there any better way to use tableLayout with recycler View without using any library?
Thanks..!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
burhan sabir
  • 51
  • 1
  • 9
  • is problem solved? – Radesh Oct 07 '18 at 11:18
  • Above mentioned problem was solved. But getting another error: You must call removeView() on the child's parent. I tried if(tableRow.getParent() != null){tableLayout.removeAllViews}; but getting same error. – burhan sabir Oct 08 '18 at 08:16

3 Answers3

0

You are not initializing tableRow anywhere.

Matt
  • 2,953
  • 3
  • 27
  • 46
0

You must create new TableRow

Use this code

TableRow tableRow=new TableRow(context); 
tableRow.addView(deposit);
    tableRow.addView(purchase);
    tableRow.addView(date);
    if(tableRow.getParent()!=null){
        ((ViewGroup)tableRow.getParent()).removeView(tableRow);
    }
    tableLayout.addView(tableRow, new TableLayout.LayoutParams(
            TableLayout.LayoutParams.FILL_PARENT,
            TableLayout.LayoutParams.WRAP_CONTENT));
Radesh
  • 13,084
  • 4
  • 51
  • 64
0

TableRow is an instance variable, so JAVA will initialize this with null value at runtime and you can't call any method with null value. You have to assign an object to the TableRow reference variable.

like:

tableRow=new TableRow(context); 
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
  • i have added "tableRow = new TableRow(context);" in bookViewHolder and removeAllViews() in Bind method like : date.setText(dataClass.date); tableRow.removeAllViews(); tableRow.addView(deposit); But still getting error:The specified child already has a parent. You must call removeView() on the child's parent first. – burhan sabir Oct 06 '18 at 17:04
  • You are adding same object again and again that's why you are getting error You must call removeView() on the child's parent , You need to change the code. In recycle view it reuse the object so you can not add same object agin in TableView. I am referring these objects tableRow.addView(deposit); tableRow.addView(purchase); tableRow.addView(date); – prashanna Gupta Oct 06 '18 at 18:05
  • but in my case list item or recyclable object is tableRow not deposit, purchase and amount views. even if i add only one view in tableRow, it gives the same error. – burhan sabir Oct 06 '18 at 18:22