0

ListView Adapter Class:

        public class ListViewAdapter extends ArrayAdapter {
            private Context context;
            private int resource;
            private ArrayList<HashMap<String,String>> list2 = new ArrayList<>();

            public ListViewAdapter(Context context,int resource, List<HashMap<String, String>> list) {
                super(context, resource, list);
                this.resource = resource;
                this.context = context;
                this.list2 = (ArrayList<HashMap<String, String>>) list;
            }
            @Override
            public View getView(int position, View convertView, ViewGroup parent){
                View row = convertView;
                ViewHolder holder;
                Object newMap = list2.get(position);
                if (row == null){
                    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                    row = inflater.inflate(resource, parent, false);
                    holder = new ViewHolder();
                    holder.name1 = (TextView) row.findViewById(R.id.name);
                    holder.add = (TextView) row.findViewById(R.id.address);
                    row.setTag(holder);
                } else {
                    holder = (ViewHolder) row.getTag();
                }  
                String pname = ((HashMap<String, String>) newMap).get("place_name");
                holder.name1.setText(pname);
                return row;
            }
            public class ViewHolder {
                TextView name1,add;
            }
}

All data coming in ListView Adapter class. But when we try to show data into list view it's got a crash. The Crash shown here holder.name1.setText(pname) in ListView Adapter class. And error show NullPointerException

Ramesh sambu
  • 3,577
  • 2
  • 24
  • 39
  • 1
    Share your error log? – Ramesh sambu Dec 26 '17 at 05:44
  • 2
    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) – ADM Dec 26 '17 at 05:44
  • AndroidRuntime: FATAL EXCEPTION: main Process: com.example.sword.google_map, PID: 8846 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.sword.google_map.ListViewAdapter.getView(ListViewAdapter.java:62) – Shashi Kushwaha Dec 26 '17 at 05:47
  • Show where you have used the Adapter! – Xenolion Dec 26 '17 at 05:49
  • private ListViewAdapter listViewAdapter; ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_view); listView = (ListView) findViewById(R.id.lv); List> placesList = new ArrayList<>(); List> list = MapsActivity.placesList; listViewAdapter = new ListViewAdapter(this,R.layout.activity_list_view,list); listView.setAdapter(listViewAdapter); } } – Shashi Kushwaha Dec 26 '17 at 06:03
  • Is `R.layout.activity_list_view` your `ListView` item layout resource? Does it contain `TextView`s with id `name` and `address`? – K Neeraj Lal Dec 26 '17 at 06:04
  • No, its only contain listView. For name & address I use another xml. – Shashi Kushwaha Dec 26 '17 at 06:09
  • Hi Shashi, Check my answer and Let me know... :) – Chetan Kumar Patel Dec 26 '17 at 06:22

4 Answers4

0

add .setText() in if condition error occurred because you initialize Textview name1 in if condition,so check below code

 @Override
        public View getView(int position, View convertView, ViewGroup parent){
            View row = convertView;
            ViewHolder holder;
            Object newMap = list2.get(position);
            if (row == null){
                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                row = inflater.inflate(resource, parent, false);
                holder = new ViewHolder();
                holder.name1 = (TextView) row.findViewById(R.id.name);
                holder.add = (TextView) row.findViewById(R.id.address);
                String pname = ((HashMap<String, String>) newMap).get("place_name");
                holder.name1.setText(pname);
                row.setTag(holder);
            } else {
                holder = (ViewHolder) row.getTag();
            }  

            return row;
        }
Omkar
  • 3,040
  • 1
  • 22
  • 42
0

As per my understanding, there is tow possible reason to show null pointer exception one is you may be getting null value in 'place_name' and another one is your referenced R.id.name is not available.

Edit your code like following,

String pname="";
pname = ((HashMap<String, String>) newMap).get("place_name");
if(holder.name1!=null){
holder.name1.setText(pname)
}
Dhaval Solanki
  • 4,589
  • 1
  • 23
  • 39
0

Finally resolve this problem, silly mistake I have does not add TextView in R.layout.activity_list_view.

0

There is no problem in your adapter except one error that is

add below code in your ListViewAdapter class

like this

@Override
public int getCount() {
    return list2.size();
}

Hope this will work for you...

  • No its doesnot work...The resolve this problem by adding EditText in R.layout.activity_list_view – Shashi Kushwaha Dec 26 '17 at 06:25
  • But you are mention textview in adapter and also you don't show your row item XML that's why i unable to recognize what is an exact issue. anyway, your problem is solved or not. please let me know ?? – Chetan Kumar Patel Dec 26 '17 at 06:34