Bav Bav - 1 month ago 13
Android Question

java.lang.NullPointerException: Attempt to invoke virtual method' on a null object reference

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;
}

}

amp amp
Answer

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