0

i am a new android programmer how to solve null pointer exception in getView method.In sqlite value did not get in list view object.

Heading ##CustomerAdapter

package com.example.extra;

import java.util.List;

import com.example.dto.DriverSelectedRouteArrayList;
import com.example.main.R;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class CustomerAdapter extends ArrayAdapter<DriverSelectedRouteArrayList>
{

    DriverSelectedRouteArrayList driverSelectedRouteArrayList;
    private List<DriverSelectedRouteArrayList> list;
    private LayoutInflater layoutInflater;
    Context context;

    public CustomerAdapter(Context context, int resource,
            List<DriverSelectedRouteArrayList> objects) 
    {
        super(context, resource,objects);
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;
        if(convertView == null)
        {
            convertView = layoutInflater.inflate(R.layout.student_list_view_itom, null);
            holder = new ViewHolder();
            holder.srNumberTextView = (TextView) convertView.findViewById(R.id.srNumberTextView);
            holder.nameTextView = (TextView) convertView.findViewById(R.id.textViewStudentItem);
            holder.phoneNumberTextView = (TextView) convertView.findViewById(R.id.textViewPhoneNumber);
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBoxSelectForStudentList);
            holder.checkBox
                            .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                                @Override
                                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
                                {
                                    int getPosition = (Integer) buttonView.getTag();
                                    list.get(getPosition).setSelected(buttonView.isChecked());

                                }
                            });
                            convertView.setTag(holder);
                            convertView.setTag(R.id.srNumberTextView,holder.srNumberTextView);
                            convertView.setTag(R.id.textViewStudentItem, holder.nameTextView);
                            convertView.setTag(R.id.textViewPhoneNumber, holder.phoneNumberTextView);

        }else{
                holder = (ViewHolder) convertView.getTag();
             }
        holder.checkBox.setTag(position);
        Log.e("get position=======>", ""+list.get(position));
        holder.srNumberTextView.setText(list.get(position).get_id());
        holder.nameTextView.setText(list.get(position).getFirst_name());
        holder.phoneNumberTextView.setText(list.get(position).getPhone_number());
        holder.checkBox.setChecked(list.get(position).isSelected());
        return convertView;
    }

    static class ViewHolder
    {
        protected TextView srNumberTextView;
        protected TextView nameTextView;
        protected TextView phoneNumberTextView;
        protected CheckBox checkBox;
    }
}

Heading ##StudentCheckBoxAndListShow

package com.example.main;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import com.example.dao.StudentListDatabaseHelper;
import com.example.dto.DriverSelectedRouteArrayList;
import com.example.extra.CustomerAdapter;
import com.example.input.DriverSelectedRouteinput;
import com.example.url.UrlAccess;
import com.example.webservcie.ParseDriverSelectedRouteId;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class StudentCheckBoxAndListShow extends Activity implements OnItemClickListener
{
    int layout = R.layout.student_list_view_itom;
    //private String SERVICE_URL = "http://10.0.2.2:8080/SBTsystem/service/viewAllStudentlist";
    UrlAccess urlAccess = new UrlAccess();
    String urlAccessStringVariable = urlAccess.SERVICE_URL;
    String SERVICE_URL = urlAccessStringVariable+"/viewAllStudentlist";

    ProgressDialog progressDialog;

    HttpPost httpPost;
    HttpResponse httpResponse;
    HttpClient httpclient ;
    StatusLine statusLine;


    TextView srNumberTextView;
    ListView listView;
    CustomerAdapter customerAdapter;

    Resources resources;

    List<DriverSelectedRouteArrayList> items;

    DriverSelectedRouteArrayList driverSelectedRouteArrayList = new DriverSelectedRouteArrayList();
    ParseDriverSelectedRouteId parseDriverSelectedRouteId = new ParseDriverSelectedRouteId();


    public ArrayList<DriverSelectedRouteArrayList> studentsListContact = new ArrayList<DriverSelectedRouteArrayList>();


//  CustomAdapter customAdapter;
    StudentListDatabaseHelper studentListDatabaseHelper;

//  oncreate method override
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.student_list_view_show);
        studentListDatabaseHelper = new StudentListDatabaseHelper(this);

        resources = getResources();
        srNumberTextView = (TextView) findViewById(R.id.srNumberTextView);
        listView = (ListView) findViewById(R.id.studentlistView);



        httpclient = new DefaultHttpClient();
//      call async task class       
        new StudentListShowAsyncTask().execute();


    }



    private void populateListView(Resources resource,ListView listView)
    {

    }

    private class StudentListShowAsyncTask extends AsyncTask<String, String, String> 
    {
        //this.getIntent().getExtras();

        @Override
        protected void onPreExecute() 
        {
            // TODO Auto-generated method stub
            super.onPreExecute();
            progressDialog = ProgressDialog.show(StudentCheckBoxAndListShow.this,""
                                ,"Loading. Please wait...", true);
            progressDialog.show();

            Log.d("on pre execute method=====>", "on preexcute");
        }

        @Override
        protected String doInBackground(String... params)
        {
            Bundle extras = getIntent().getExtras();
            String routeIdFromDriverSelected = extras.getString("routeId");

            try {
                httpPost = new HttpPost(SERVICE_URL);

                DriverSelectedRouteinput driverSelectedRouteinput = new DriverSelectedRouteinput();
                String jsonRequest = driverSelectedRouteinput.createjsonRequestForDriverSelectedRoute(routeIdFromDriverSelected);

                StringEntity stringEntety;
                stringEntety = new StringEntity(jsonRequest);

                stringEntety.setContentEncoding("UTF-8");
                stringEntety.setContentType("application/json");

                httpPost.setEntity(stringEntety);

                httpResponse = httpclient.execute(httpPost);
                statusLine = httpResponse.getStatusLine();

                int statusCode = statusLine.getStatusCode();

                if(statusCode == 200)
                    {
                        Log.d("status code", statusCode+"");
                        driverSelectedRouteArrayList = parseDriverSelectedRouteId.parseResponseForRouteIdResult(httpResponse);

                        /*int arrayListSize = parseDriverSelectedRouteId.studentList.size();
                        for(int i =0; i<arrayListSize; i++)
                        {
                        Log.d("response first name=-=-==-===-=-=-=-=>"+i, driverSelectedRouteArrayList.getFirst_name()+"");
                        }*/
                        //String routeId = driverSelectedRouteArrayList.getRoute_id();
                        //Log.d("route id------>", routeId);

                        StudentListDatabaseHelper studentListDatabaseHelper = new StudentListDatabaseHelper(getBaseContext());
                        //studentListDatabaseHelper.getWritableDatabase();
                        studentListDatabaseHelper.insertRecord( driverSelectedRouteArrayList);

                        //select sqlite database in android
                        items = studentListDatabaseHelper.selectRecord();
                        //int _id = driverSelectedRouteArrayList.get_id();
                        /*String _idString = ""+_id;
                        Log.d("id get from the sqlite database-==-=-=-=>",_idString);*/
                        Log.e("database created=========>>", items+"sqlite database created");
                    }
                //Log.d("status code", statusCode+"");
                } catch (Exception e)
                {
                    e.printStackTrace();

                }
            return null;
        }
        @Override
        protected void onPostExecute(String result)
        {
            progressDialog.dismiss();
            super.onPostExecute(result);

            setAdapterToListview();
        }

    }

//  set setAdapterToListview
    public void setAdapterToListview()
    {
        Log.e("set adpter items value====>", ""+items);
        Collections.sort(items,new Comparator<DriverSelectedRouteArrayList>() {

            @Override
            public int compare(DriverSelectedRouteArrayList lhs,
                    DriverSelectedRouteArrayList rhs) 
            {

                return lhs.getFirst_name().compareTo(rhs.getFirst_name());
            }

        });
        customerAdapter = new CustomerAdapter(StudentCheckBoxAndListShow.this,MODE_MULTI_PROCESS, items);
        Log.e("items==========>>>>>>>>>>>>>>", ""+items);
        listView.setAdapter(customerAdapter);
        listView.setOnItemClickListener(new OnItemClickListener() 
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id)
            {
                CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBoxSelectForStudentList);
                DriverSelectedRouteArrayList listViewStudent = items.get(position);
                if(listViewStudent.isSelected())
                {
                    listViewStudent.setSelected(false);
                    checkBox.setChecked(false);
                }else
                    {   
                        listViewStudent.setSelected(true);
                        checkBox.setChecked(true);
                    }
            }

        });
    }
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
    {
        DriverSelectedRouteArrayList tempValues = studentsListContact.get(arg2);
        Toast.makeText(this, ""+tempValues.getFirst_name()+"phone number"+tempValues.getPhone_number(), Toast.LENGTH_LONG).show();
    }
}

Heading ##DriverSelectedRouteArrayList

package com.example.dto;

import java.util.ArrayList;
import java.util.List;

public class DriverSelectedRouteArrayList 
{
//  ArrayList arrayList = new ArrayList();
    ArrayList<String> studentArrayList = new ArrayList<String>();

//  create list view object 
    public List<DriverSelectedRouteArrayList> driverSelectedRouteArrayList;

    public int _id;
    public String first_name;
    public String middle_name;
    public String last_name;
    public String route_id;
    public String phone_number;
    boolean selected;

    public DriverSelectedRouteArrayList()
    {
        first_name = null;
        middle_name = null;
        last_name = null;
        route_id = null;
        phone_number = null;

    }
    public int get_id() 
    {
        return _id;
    }

    public int set_id(int _id) 
    {
        return this._id = _id;
    }
    public ArrayList<String> getStudentArrayList() 
    {
        return studentArrayList;
    }
    public void setStudentArrayList(ArrayList<String> studentArrayList) 
    {
        this.studentArrayList = studentArrayList;
    }

    public String getFirst_name() {
        return first_name;
    }

    public String setFirst_name(String first_name) {
        return this.first_name = first_name;
    }

    public String getMiddle_name()
    {
        return middle_name;
    }
    public String setMiddle_name(String middle_name) 
    {
        return this.middle_name = middle_name;
    }
    public String getLast_name() 
    {
        return last_name;
    }
    public String setLast_name(String last_name) 
    {
        return this.last_name = last_name;
    }
    public String getRoute_id()
    {
        return route_id;
    }
    public String setRoute_id(String routeId)
    {
        return this.route_id = routeId;
    }
    public String getPhone_number() 
    {
        return phone_number;
    }
    public String setPhone_number(String phone_number) 
    {
        return this.phone_number = phone_number;
    }
    public boolean isSelected() {
        return selected;
    }
    public void setSelected(boolean selected) {
        this.selected = selected;
    }
    public List<DriverSelectedRouteArrayList> getDriverSelectedRouteArrayList() 
    {
        return driverSelectedRouteArrayList;
    }
    public void setDriverSelectedRouteArrayList(
            List<DriverSelectedRouteArrayList> driverSelectedRouteArrayList) 
    {
        this.driverSelectedRouteArrayList = driverSelectedRouteArrayList;
    }

}

Heading ##logcat

01-03 18:30:07.862: E/AndroidRuntime(3989): FATAL EXCEPTION: main
01-03 18:30:07.862: E/AndroidRuntime(3989): java.lang.NullPointerException
01-03 18:30:07.862: E/AndroidRuntime(3989):     at com.example.extra.CustomerAdapter.getView(CustomerAdapter.java:65)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at android.widget.AbsListView.obtainView(AbsListView.java:2452)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at android.widget.ListView.onMeasure(ListView.java:1161)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at android.view.View.measure(View.java:12865)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:579)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:392)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at android.view.View.measure(View.java:12865)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at android.view.View.measure(View.java:12865)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:822)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at android.view.View.measure(View.java:12865)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4712)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2242)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at android.view.View.measure(View.java:12865)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1240)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at android.os.Looper.loop(Looper.java:137)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at android.app.ActivityThread.main(ActivityThread.java:4517)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at java.lang.reflect.Method.invokeNative(Native Method)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at java.lang.reflect.Method.invoke(Method.java:511)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
01-03 18:30:07.862: E/AndroidRuntime(3989):     at dalvik.system.NativeStart.main(Native Method)

Heading ##second logcat to solve differnet problem

01-03 18:51:17.792: E/AndroidRuntime(7628): FATAL EXCEPTION: main
01-03 18:51:17.792: E/AndroidRuntime(7628): java.lang.NullPointerException
01-03 18:51:17.792: E/AndroidRuntime(7628):     at java.util.Collections.sort(Collections.java:1890)
01-03 18:51:17.792: E/AndroidRuntime(7628):     at com.example.main.StudentCheckBoxAndListShow.setAdapterToListview(StudentCheckBoxAndListShow.java:188)
01-03 18:51:17.792: E/AndroidRuntime(7628):     at com.example.main.StudentCheckBoxAndListShow$StudentListShowAsyncTask.onPostExecute(StudentCheckBoxAndListShow.java:179)
01-03 18:51:17.792: E/AndroidRuntime(7628):     at com.example.main.StudentCheckBoxAndListShow$StudentListShowAsyncTask.onPostExecute(StudentCheckBoxAndListShow.java:1)
01-03 18:51:17.792: E/AndroidRuntime(7628):     at android.os.AsyncTask.finish(AsyncTask.java:602)
01-03 18:51:17.792: E/AndroidRuntime(7628):     at android.os.AsyncTask.access$600(AsyncTask.java:156)
01-03 18:51:17.792: E/AndroidRuntime(7628):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
01-03 18:51:17.792: E/AndroidRuntime(7628):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 18:51:17.792: E/AndroidRuntime(7628):     at android.os.Looper.loop(Looper.java:137)
01-03 18:51:17.792: E/AndroidRuntime(7628):     at android.app.ActivityThread.main(ActivityThread.java:4517)
01-03 18:51:17.792: E/AndroidRuntime(7628):     at java.lang.reflect.Method.invokeNative(Native Method)
01-03 18:51:17.792: E/AndroidRuntime(7628):     at java.lang.reflect.Method.invoke(Method.java:511)
01-03 18:51:17.792: E/AndroidRuntime(7628):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
01-03 18:51:17.792: E/AndroidRuntime(7628):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
01-03 18:51:17.792: E/AndroidRuntime(7628):     at dalvik.system.NativeStart.main(Native Method)
2
  • In getView very first line error. Commented Jan 3, 2014 at 13:02
  • boolean value not accept.its GropView object. Commented Jan 3, 2014 at 13:06

1 Answer 1

3

You have not initialised private "list" object.
And trying to use at line 65

Log.e("get position=======>", ""+list.get(position));

Solution

public CustomerAdapter(Context context, int resource,
            List<DriverSelectedRouteArrayList> objects) 
{
     super(context, resource,objects);
     layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     list = objects;
}
Sign up to request clarification or add additional context in comments.

3 Comments

nice catch +1 from me
its correct dude line 65 this is but how to solve error In line number 65
@Nitin just save the passed reference in constructor as "list = objects"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.