-2

I want to highlight a certain row in a list view based on a certain condition being true.I tried the following code and I get a Null Pointer exception.

public class BillsCustomList extends ArrayAdapter<String>
{
    private String[] doc_no;
    private String[]date;
    private String[] cust_name;
    private String[] cust_number;
    private String[] item_count;
    private String[]total_wt;
    private String[]total;
    private String[]balance;
    private String[]bill_type;
    private String[]status;

    private Activity context;

    public BillsCustomList(Activity context, String[] doc_no, String[] date, String[] cust_name,String[] cust_number,
                           String[] item_count,String[] total_wt,String[] total,String[] balance,String[] bill_type,String[] status)
    {
        super(context, R.layout.bills_list_view, doc_no);
        this.context =context;
        this.doc_no=doc_no;
        this.date = date;
        this.cust_name = cust_name;
        this.cust_number = cust_number;
        this.item_count= item_count;
        this.total_wt = total_wt;
        this.total = total;
        this.balance = balance;
        this.bill_type = bill_type;
        this.status = status;

    }

    @Override
    public View getView(int position, View listViewItem, ViewGroup parent)
    {
        if (null == listViewItem) {
            LayoutInflater inflater = context.getLayoutInflater();
            listViewItem = inflater.inflate(R.layout.bills_list_view, null, true);
        }
        TextView textViewDocno = (TextView) listViewItem.findViewById(R.id.textViewInvNo);
        TextView textViewDt = (TextView) listViewItem.findViewById(R.id.textViewDt);
        TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);
        TextView textViewNumber = (TextView) listViewItem.findViewById(R.id.textViewNumber);
        TextView textViewCount = (TextView) listViewItem.findViewById(R.id.textViewCount);
        TextView textViewTotwt = (TextView) listViewItem.findViewById(R.id.textViewTotwt);
        TextView textViewTot = (TextView) listViewItem.findViewById(R.id.textViewTot);
        TextView textViewBal = (TextView) listViewItem.findViewById(R.id.textViewBalanace);
        TextView textViewBt = (TextView) listViewItem.findViewById(R.id.textViewBt);
        TextView textViewStatus = (TextView) listViewItem.findViewById(R.id.textView_status);
        LinearLayout ll = (LinearLayout) listViewItem.findViewById(R.id.ll_bills);

        textViewDocno.setText(doc_no[position]);
        textViewDt.setText(date[position]);
        textViewName.setText(cust_name[position]);
        textViewNumber.setText(cust_number[position]);
        textViewCount.setText(item_count[position]);
        textViewTotwt.setText(total_wt[position]);
        textViewTot.setText(total[position]);
        textViewBal.setText(balance[position]);
        textViewBt.setText(bill_type[position]);
        textViewStatus.setText(status[position]);

        try
        {
            if (textViewStatus.getText().toString().equalsIgnoreCase("true"))
            {
                ll.setBackgroundColor(Color.RED);

            }

        }catch (NullPointerException e)
        {
            Log.d("NULL ERROR", "position"+position+e);
        }




        return listViewItem;
    }


}

I have simple use case if the status is true make the row of a certain color.Else don't do anything.I get the following error when I try the above code.

NULL ERROR: position0java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference

I get these null pointer exceptions at 0 and 1 position.Can anyone explain why is this happening and what would be the correct way to do it. Any help or suggestion is welcome.Thank you.

AndroidNewBee
  • 744
  • 3
  • 12
  • 36

3 Answers3

1

May be you are gettign status[position] null for some positions to avoid that change your code like following

if(textViewStatus.getText()!=null){

     if (textViewStatus.getText().toString().equalsIgnoreCase("true"))
     {
        ll.setBackgroundColor(Color.RED);

     }else{
        ll.setBackgroundColor("Different color");
     }
}else{
      ll.setBackgroundColor("Different color");
}
mdDroid
  • 3,135
  • 2
  • 22
  • 34
1

Well @mdDroid has answered question, but there is some other standard way to check null strings.

TextUtils.isEmpty(CharSequence str) - Returns true if the string is null or 0-length.

if (!TextUtils.isEmpty(textViewStatus.getText()) && textViewStatus.getText().toString().equalsIgnoreCase("true"))
    ll.setBackgroundColor(Color.RED);
else
    ll.setBackgroundResource(android.R.color.transparent);

The best way to avoid NPE for strings by writing conditions like below

 if ("true".equalsIgnoreCase(textViewStatus.getText().toString()))
     ll.setBackgroundColor(Color.RED);
 else
     ll.setBackgroundResource(android.R.color.transparent);
Bharatesh
  • 8,943
  • 3
  • 38
  • 67
0

You should check array position on array before background change:

if (position < status.length() && status[position].equalsIgnoreCase("true"))
    ll.setBackgroundColor(Color.RED);
else
    ll.setBackgroundResource(android.R.color.transparent);

i hope all arrays has the same length, or the ArrayAdapter logic doesn't work properly

mcatta
  • 481
  • 5
  • 12