-1

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.

User 10
  • 129
  • 5
  • 11
  • @stealthjong `delete_btn.setOnClickListener(new OnClickListener() { ---->Null pointer Exception` I am mentioned already – User 10 Dec 22 '14 at 15:21
  • Try cleaning your project (Project -> Clean) – Mohamed_AbdAllah Dec 22 '14 at 15:23
  • your delete_btn is not a part of your main layout which is where you're trying to locate it (and obviously, it throws a NPE). It's part of the layout that's inflated in the adapter and is used as a list view item (you can have multiples of these). Use a custom adapter and define your delete logic there (or use an interface to do it in the activity). An example here: https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView – kha Dec 22 '14 at 15:27

2 Answers2

0

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: ,

Blue_Alien
  • 2,148
  • 2
  • 25
  • 29
  • I knew that already.please read the question carefully.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.` – User 10 Dec 22 '14 at 15:25
  • Any suggestion regarding to this. – User 10 Dec 22 '14 at 15:26
  • Your whole idea of the layout is messed up. You can't get button from another layout and try to use it somewhere else – Rohit5k2 Dec 22 '14 at 15:27
  • we wrote the answer for your current question, if you want to set onclick listener to button which is created with view_member_entry layout, then set the onclick listener to the button delete_btn from the custome adapter! this is not the way to add onclick listener to this button – Blue_Alien Dec 22 '14 at 15:29
  • as you need a custome layout for the listview it is better to go through a custome adapter. – Blue_Alien Dec 22 '14 at 15:33
0

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.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57