Null pointer exception on delete_btn

56 Views Asked by At

I am getting a null pointer exception at delete_btn.I am =mentioned the error line in below codes and I am posted the related coded to that.

Stacktrace:

12-22 10:03:59.897: E/AndroidRuntime(1505): FATAL EXCEPTION: main
12-22 10:03:59.897: E/AndroidRuntime(1505): Process: com.example.sqlitedemoo, PID: 1505
12-22 10:03:59.897: E/AndroidRuntime(1505): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqlitedemoo/com.pavan.sqlitedemoo.MainActivity}: java.lang.NullPointerException
12-22 10:03:59.897: E/AndroidRuntime(1505):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-22 10:03:59.897: E/AndroidRuntime(1505):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-22 10:03:59.897: E/AndroidRuntime(1505):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-22 10:03:59.897: E/AndroidRuntime(1505):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-22 10:03:59.897: E/AndroidRuntime(1505):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-22 10:03:59.897: E/AndroidRuntime(1505):     at android.os.Looper.loop(Looper.java:136)
12-22 10:03:59.897: E/AndroidRuntime(1505):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-22 10:03:59.897: E/AndroidRuntime(1505):     at java.lang.reflect.Method.invokeNative(Native Method)
12-22 10:03:59.897: E/AndroidRuntime(1505):     at java.lang.reflect.Method.invoke(Method.java:515)
12-22 10:03:59.897: E/AndroidRuntime(1505):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-22 10:03:59.897: E/AndroidRuntime(1505):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-22 10:03:59.897: E/AndroidRuntime(1505):     at dalvik.system.NativeStart.main(Native Method)
12-22 10:03:59.897: E/AndroidRuntime(1505): Caused by: java.lang.NullPointerException
12-22 10:03:59.897: E/AndroidRuntime(1505):     at com.pavan.sqlitedemoo.MainActivity.onCreate(MainActivity.java:50)
12-22 10:03:59.897: E/AndroidRuntime(1505):     at android.app.Activity.performCreate(Activity.java:5231)
12-22 10:03:59.897: E/AndroidRuntime(1505):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-22 10:03:59.897: E/AndroidRuntime(1505):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
12-22 10:03:59.897: E/AndroidRuntime(1505):     ... 11 more

MainActivity.java:

public class MainActivity extends Activity {

    ListView lv;
    SQLController dbcon;
    TextView memID_tv, memName_tv;

    Button delete_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbcon = new SQLController(this);
        dbcon.open();
        lv = (ListView) findViewById(R.id.memberList_id);
        delete_btn = (Button) findViewById(R.id.delete_btn);

        // Attach The Data From DataBase Into ListView Using Crusor Adapter
        Cursor cursor = dbcon.readData();
        String[] from = new String[] { DBhelper.MEMBER_ID, DBhelper.MEMBER_NAME };
        int[] to = new int[] { R.id.member_id, R.id.member_name };

        @SuppressWarnings("deprecation")
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                MainActivity.this, R.layout.view_member_entry, cursor, from, to);

        adapter.notifyDataSetChanged();
        lv.setAdapter(adapter);


        delete_btn.setOnClickListener(new OnClickListener() { --> 50th Line- Null pointer Exception
            @Override
            public void onClick(View v) {

                memID_tv = (TextView) v.findViewById(R.id.member_id);
                memName_tv = (TextView) v.findViewById(R.id.member_name);

                String memberID_val = memID_tv.getText().toString();
                String memberName_val = memName_tv.getText().toString();

                Intent modify_intent = new Intent(getApplicationContext(),
                        Modify_member.class);
                modify_intent.putExtra("memberName", memberName_val);
                modify_intent.putExtra("memberID", memberID_val);
                startActivity(modify_intent);
            }
        });

    }

What I need:

I am added the Delete button in adaper xml(view_member_entry.xml).Because I need to perform delete operation for each list view row items.Anybody can help me with this.Thank you.

2

There are 2 best solutions below

5
On BEST ANSWER

the layout file of main activity is activity_main.xml: and it is not having a element as button with name delete_btn , but there is a button as the element of view_member_entry.xml: ,

0
On

You are loading activity_main in the activity and trying to get delete_btn from there. But there isn't any delete_btn in that layout xml. Check your code.