-3

I want to retrieve id from SQLiteDatabase when click on ListView item in next activity. When I run the application it getting NullPointerException.

I have declare id in String in DataBase_Adapter class:

public static final String KEY_NEW_LEAD_ID ="id";

Here is my ListView Code:

displayDataList = (ListView)findViewById(R.id.listViewGetAllData);

displayDataList.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
        // TODO Auto-generated method stub

        Intent ii=new Intent(Display_All_Data.this ,New_Lead_Detail_Activity.class);
        cursor = (Cursor) displayDataAdapter.getItem(position);
        int i=cursor.getInt(cursor.getColumnIndex("id"));
        System.out.println("ID = " + i);
        ii.putExtra("NEW_LEAD_USER_ID", i);
        startActivity(ii);
    }
});

Here is my Log Cat Info:

12-17 17:50:26.024: E/AndroidRuntime(10322): FATAL EXCEPTION: main
12-17 17:50:26.024: E/AndroidRuntime(10322): java.lang.NullPointerException
12-17 17:50:26.024: E/AndroidRuntime(10322):    at com.lead_management_project.Display_All_Data$1.onItemClick(Display_All_Data.java:48)
12-17 17:50:26.024: E/AndroidRuntime(10322):    at android.widget.AdapterView.performItemClick(AdapterView.java:284)
12-17 17:50:26.024: E/AndroidRuntime(10322):    at android.widget.ListView.performItemClick(ListView.java:3513)
12-17 17:50:26.024: E/AndroidRuntime(10322):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
12-17 17:50:26.024: E/AndroidRuntime(10322):    at android.os.Handler.handleCallback(Handler.java:587)
12-17 17:50:26.024: E/AndroidRuntime(10322):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-17 17:50:26.024: E/AndroidRuntime(10322):    at android.os.Looper.loop(Looper.java:123)
12-17 17:50:26.024: E/AndroidRuntime(10322):    at android.app.ActivityThread.main(ActivityThread.java:3683)
12-17 17:50:26.024: E/AndroidRuntime(10322):    at java.lang.reflect.Method.invokeNative(Native Method)
12-17 17:50:26.024: E/AndroidRuntime(10322):    at java.lang.reflect.Method.invoke(Method.java:507)
12-17 17:50:26.024: E/AndroidRuntime(10322):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-17 17:50:26.024: E/AndroidRuntime(10322):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-17 17:50:26.024: E/AndroidRuntime(10322):    at dalvik.system.NativeStart.main(Native Method)

This my Display_Data_Adapter class extends BaseAdapter

public class Display_Data_Adapter extends BaseAdapter
{
    Context mContext;
    ArrayList<String> newLead_ArrayList_OrgName;
    ArrayList<String> newLead_ArrayList_Name;
    ArrayList<String> newLead_ArrayList_Status;
    ArrayList<String> newLead_ArrayList_Budget;

    /**
     * @param mContext
     * @param newLead_ArrayList_OrgName
     * @param newLead_ArrayList_Name
     * @param newLead_ArrayList_Status
     * @param newLead_ArrayList_Budget
     */
    protected Display_Data_Adapter(Context mContext,
            ArrayList<String> newLead_ArrayList_OrgName,
            ArrayList<String> newLead_ArrayList_Name,
            ArrayList<String> newLead_ArrayList_Status,
            ArrayList<String> newLead_ArrayList_Budget) {
        this.mContext = mContext;
        this.newLead_ArrayList_OrgName = newLead_ArrayList_OrgName;
        this.newLead_ArrayList_Name = newLead_ArrayList_Name;
        this.newLead_ArrayList_Status = newLead_ArrayList_Status;
        this.newLead_ArrayList_Budget = newLead_ArrayList_Budget;
    }



    /**
     * 
     */
    protected Display_Data_Adapter() {
        super();
        // TODO Auto-generated constructor stub
    }


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

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

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

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

        Holder mHolder;
        LayoutInflater layoutInflater;

        if (v == null)
        {
            layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = layoutInflater.inflate(R.layout.search_data_item_listview,null);

            mHolder = new Holder();

            mHolder.txt_newLead_OrgName = (TextView) v
                    .findViewById(R.id.textvieworgname);

            mHolder.txt_newLead_Name = (TextView) v
                    .findViewById(R.id.textviewname);

            mHolder.txt_newLead_Status = (TextView) v
                    .findViewById(R.id.textviewstatus);


            mHolder.txt_newLead_Budget = (TextView) v
                    .findViewById(R.id.textviewbudget);

            v.setTag(mHolder);

        } else {

            mHolder = (Holder) v.getTag();

        }



        mHolder.txt_newLead_OrgName.setText(newLead_ArrayList_OrgName.get(position));

        mHolder.txt_newLead_Name.setText(newLead_ArrayList_Name.get(position));

        mHolder.txt_newLead_Status.setText(newLead_ArrayList_Status.get(position));

        mHolder.txt_newLead_Budget.setText(newLead_ArrayList_Budget.get(position));

        return v;


    }

    public class Holder {

        TextView txt_newLead_OrgName;
        TextView txt_newLead_Name;
        TextView txt_newLead_Status;
        TextView txt_newLead_Budget;

    }

}
halfer
  • 19,824
  • 17
  • 99
  • 186
tazeenmulani
  • 600
  • 5
  • 18
  • 43
  • You have a NullPointerException on line 48. What line is this? Might it be that your cursor is null? – mvieghofer Dec 17 '13 at 12:30
  • 1
    `displayDataAdapter` is `null` ? change `displayDataAdapter.getItem` it to `arg0.getItemAtPosition` ... and SO is not debuging online service ... posting NPE in own code is laziness – Selvin Dec 17 '13 at 12:31
  • I didn't get you.Can you please explain me ? – tazeenmulani Dec 17 '13 at 12:35
  • @mvieghofer at line 48 = int i=cursor.getInt(cursor.getColumnIndex("id")); – tazeenmulani Dec 17 '13 at 12:36
  • you have to open your database before retrieve data from database. – Piyush Dec 17 '13 at 12:38
  • @Selvin, If u want to answer my code then answer otherwise don't call my code laziness. I'm not experienced one like you. – tazeenmulani Dec 17 '13 at 12:55
  • you didn't understand me ... not "your code is laziness" but posting question about NPE in your own code ... you should debug your code: first check what is null and then think why it could be null by yourself and then eventually ask here why something is null ... – Selvin Dec 17 '13 at 13:01
  • anway this question is wrong asked at so many points of view ... 1. you asked about NPE in your own code 2. you didn't point where NPE appeared 3. you showed lack of logical thinking: In logcat is written "NPE at Display_All_Data.java:48" ... so, if line 48 is `cursor = (Cursor) displayDataAdapter.getItem(position);` only the cursor can be null ... if so displayDataAdapter.getItem(position) returned null ... if so 4. you didn't provide what is `displayDataAdapter` and how looks `displayDataAdapter.getItem` ... – Selvin Dec 17 '13 at 13:10
  • @Selvin , displayDataAdapter is the class which is extended BaseAdapter. – tazeenmulani Dec 18 '13 at 06:31

1 Answers1

1

When you get the NullPointerException at

int i=cursor.getInt(cursor.getColumnIndex("id"));

then your cursor is null. This means that

cursor = (Cursor) displayDataAdapter.getItem(position);

return null.

edit:

After some discussion in the comments, maybe the CursorAdapter is better suited for you. Using the CursorAdapter you can directly link a database cursor with a listview. See also this SO post or this tutorial that was recommended here.

Community
  • 1
  • 1
mvieghofer
  • 2,846
  • 4
  • 22
  • 51
  • Can you give me some suggestion how to do it. – tazeenmulani Dec 18 '13 at 06:12
  • Sorry but with the information you provided all I can say is that your displayDataAdapter.getItem(position) returns null. I don't know what the displayDataAdapter is. Take a look at the initialization of the data (there might be null values where no null values should be). If it is your own implementation of an adapter, maybe your getItem doesn't work correctly. You can try to debug your app and set breakpoints so that you can inspect the actual data, this should help you to identify where the problem is. – mvieghofer Dec 18 '13 at 07:56
  • displayDataAdapter is the class which is extends BaseAdapter. I tried lot of since last four hours but i can't solve this why the cursor is null.Please help me out where i'm wrong. – tazeenmulani Dec 18 '13 at 10:01
  • I have posted Display_Data_Adapter class (displayDataAdapter). – tazeenmulani Dec 18 '13 at 10:02
  • Your getItem() does nothing but returning the position you passed in. This can work... you need to return the object that is displayed at the position you passed in. Maybe this example helps you a bit http://stackoverflow.com/questions/8166497/custom-adapter-for-list-view – mvieghofer Dec 18 '13 at 10:28
  • Also I don't understand why you want to get a Cursor from the adapter. Typically if you show a list of `Employee`s, you would pass that list to the adapter of your listview and retrieve an `Employee` object from the `getItem` method. Then you could load additional data from the database. – mvieghofer Dec 18 '13 at 10:31
  • There is actually a CursorAdapter. This post maybe helps you with your problem http://stackoverflow.com/questions/12671119/how-to-get-id-from-database-on-click-of-listview-item-in-android?rq=1 – mvieghofer Dec 18 '13 at 10:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43417/discussion-between-tazeenmulani-and-mvieghofer) – tazeenmulani Dec 18 '13 at 10:47
  • @tazeenmulani I've answered you in the chat – mvieghofer Dec 18 '13 at 11:29