-2

I tried to see every answer about this question but did not work with me

I know that the counter view return null but i don not know why

Tried something like that in the code but did not worked

      public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub



    LayoutInflater inflater=activity.getLayoutInflater();

    if(convertView == null){

        convertView=inflater.inflate(R.layout.column_row, null);

        txtFirst=(TextView) convertView.findViewById(R.id.name);
        txtSecond=(TextView) convertView.findViewById(R.id.gender);
        txtThird=(TextView) convertView.findViewById(R.id.age);
        //txtFourth=(TextView) convertView.findViewById(R.id.status);

        if (txtFirst == null) {
            txtFirst.setText("kkkk");
        }
        if (txtSecond == null) {
            txtSecond.setText("kllll");
        }
        if (txtThird == null) {
            txtThird.setText("llllll");
        }

    }
    return convertView;
   }

My logcat

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

my code

package com.example.bavly.dollorvalueindifferentboanksegypt;



import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

import static      
com.example.bavly.dollorvalueindifferentboanksegypt.constants.FIRST_COLUMN;
import static   
com.example.bavly.dollorvalueindifferentboanksegypt.constants.SECOND_COLUMN;
import static   
com.example.bavly.dollorvalueindifferentboanksegypt.constants.THIRD_COLUMN;

public class ListViewAdapters extends BaseAdapter {
public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFirst;
TextView txtSecond;
TextView txtThird;
//TextView txtFourth;
public ListViewAdapters(Activity activity,ArrayList<HashMap<String, String>> list){
    super();
    this.activity=activity;
    this.list=list;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}



@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub



    LayoutInflater inflater=activity.getLayoutInflater();

    if(convertView == null){

        convertView=inflater.inflate(R.layout.column_row, null);

        txtFirst=(TextView) convertView.findViewById(R.id.name);
        txtSecond=(TextView) convertView.findViewById(R.id.gender);
        txtThird=(TextView) convertView.findViewById(R.id.age);
        //txtFourth=(TextView) convertView.findViewById(R.id.status);


    }

    HashMap<String, String> map=list.get(position);
    txtFirst.setText(map.get(FIRST_COLUMN));
    txtSecond.setText(map.get(SECOND_COLUMN));
    txtThird.setText(map.get(THIRD_COLUMN));
    //txtFourth.setText(map.get(FOURTH_COLUMN));

    return convertView;
}

}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Bav
  • 139
  • 2
  • 3
  • 14
  • Really? `if (txtFirst == null) { txtFirst.setText("kkkk"); }`. – OneCricketeer Nov 05 '16 at 21:47
  • if i removed it , i still have the same problem – Bav Nov 05 '16 at 21:49
  • There's not a ListView in your question, but it's null, you should read the exception and debug why that is. The reason for a NullPointerException is always the same – OneCricketeer Nov 05 '16 at 21:50
  • My first comment was that it would throw an error if the textview was null and the if statement was true. You can setText on a null value – OneCricketeer Nov 05 '16 at 21:52
  • @cricket_007 Thank you for your help Really , i am young developer and learn from my Mistake I found the solution there stackoverflow.com/questions/3264610/findviewbyid-returns-nul‌​l It was my mistake in main activity i was passing the wrong layout view – Bav Nov 06 '16 at 10:40

1 Answers1

1

The listview is null. Confirm that findViewById doesn't return null.

amp
  • 11,754
  • 18
  • 77
  • 133
  • Thank you I searched for what you Say I found the solution there http://stackoverflow.com/questions/3264610/findviewbyid-returns-null there was a mistake when it return back of in the Main Activity setContentView(R.layout.listvalue); – Bav Nov 06 '16 at 10:37