Search code examples
javaandroidbaseadapter

Android Image button Dial / call from a custom list view Error with NullPointerException


In my listview I added a button with icon/image and try to make a call from that button with the string number from the listview. but somehow its not working or show me error

enter image description here

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.rupom.sss.EmployeeContactList.onCreate

Here is my java code

public class EmployeeContactList  extends Activity {


    public EmployeeContactList(){}
    ListView empList;

    String[] EmpName = {
            "ROB","Fredrik","Mihai","Andru","Gob shafin","Andru","Gob shafin","Andru","Gob shafin","Andru","Gob shafin","Andru","Gob shafin"

    };
    String[] EmpDeg = {
            "Senior Officer","Junior Officer","Officer","Junior Officer","Officer","Junior Officer","Officer","Junior Officer","Officer","Junior Officer","Officer","Junior Officer","Officer"

    };
    String[] EmpMobile = {
            "1245454544552","12345678912","12345678912","12345678912","12345678912","12345678912","12345678912","12345678912","12345678912","12345678912","12345678912","12345678912","12345678912"
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_employee_list);
        empList = findViewById(R.id.employeelistView);

        CustomEmployeeListAdapter customEmployeeListAdapter = new CustomEmployeeListAdapter(getApplicationContext(), EmpName, EmpDeg,EmpMobile);
        empList.setAdapter(customEmployeeListAdapter);

    }


}

Custom list adapter

public class CustomEmployeeListAdapter extends BaseAdapter {
    Context context;
    String[] EmpName;
    String[] EmpDeg;
    String[] EmpMobile;


    LayoutInflater inflter;

    public CustomEmployeeListAdapter(Context applicationContext, String[] EmpName, String[] EmpDeg, String[] EmpMobile) {
        this.context = context;
        this.EmpName = EmpName;
        this.EmpDeg = EmpDeg;
        this.EmpMobile = EmpMobile;
        inflter = (LayoutInflater.from(applicationContext));
    }

    @Override
    public int getCount() {
        return EmpName.length;
    }

    @Override
    public Object getItem(int position) {
        return EmpName[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        String text = (String) getItem(position);
        //get the layout
        view = inflter.inflate(R.layout.list_employee, null);
        //get the fields of list view
        TextView tvName = (TextView) view.findViewById(R.id.employee_name);
        TextView tvDeg = (TextView) view.findViewById(R.id.list_sub_emp_deg);
        TextView tvMobile = (TextView) view.findViewById(R.id.list_sub_emp_mobile);



        //set value from string
        tvName.setText(EmpName[position]);
        tvDeg.setText(EmpDeg[position]);
        tvMobile.setText(EmpMobile[position]);


        final TextView mobNumber = (TextView)view.findViewById(R.id.list_sub_emp_mobile);
        final ImageView empCall = (ImageView)view.findViewById(R.id.empCallBtn);
        final String mobile_number = mobNumber.getText().toString();
        // Click listener of button
        empCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:"+mobile_number));
                //context.startActivity(callIntent);
                Log.i("PHONENUMBER","Clicked !!!!!!! "+ mobile_number);
            }
        });
        return view;
    }

}

Solution

  • You need to return the correct value for all of your BaseAdapter methods. So, update your code to something like this:

    public class CustomEmployeeListAdapter extends BaseAdapter {
        Context context;
        String[] EmpName;
        String[] EmpDeg;
        String[] EmpMobile;
    
        ...
    
        @Override
        public Object getItem(int position) {
            return EmpName[position];
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        ...
    
    }
    

    For improvement:

    You better to use a pojo which include all of your Employee information. For example, you can use Employee class:

    public class Employee {
      private String name;
      private String degree;
      private String mobile;
    
      // constructor
      // setter
      // getter
    
    }
    

    then, you can use it in your adapter:

    public class CustomEmployeeListAdapter extends BaseAdapter {
        Context context;
        List<Employee> mEmployees;
    
        ...
    
        public CustomEmployeeListAdapter(Context ctx, List<Employee> employees) {
            this.context = ctx;
            mEmployees employees;
    
            ...
        }
    
        @Override
        public int getCount() {
            return mEmployees.size();
        }
    
        @Override
        public Object getItem(int position) {
            return mEmployees.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
    
        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {
    
            //get the layout
            ...
    
            Employee employee = (Employee) getItem(position);
    
            tvName.setText(employee.getName());
            tvDeg.setText(employee.getDegree());
            tvMobile.setText(employee.getMobile());
    
            return view;
        }
    
    }