-2

I am getting Error in one of my java file of my app Error is : java.lang.NullPointerException

I'm getting on the below line,

String getName = name.getText().toString();

Here is my code, where i'm getting error in detailSave method

public class Contact_Details extends ActionBarActivity {

    Button save, show;
    EditText name, email, phnno;
    DataHandler handler;
    long id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact__details);

        save = (Button) findViewById(R.id.c_save);
        show = (Button) findViewById(R.id.c_show);
        name = (EditText) findViewById(R.id.c_name);
        email = (EditText) findViewById(R.id.c_email);
        phnno = (EditText) findViewById(R.id.c_phnno);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    /*
     * public void detailSave(View v) { String getName =
     * name.getText().toString(); String getEmail = email.getText().toString();
     * String getPhnno = phnno.getText().toString(); handler = new
     * DataHandler(getBaseContext()); handler.open(); id =
     * handler.insertData(getName, getEmail, getPhnno);
     * Toast.makeText(getBaseContext(), "Data inserted",
     * Toast.LENGTH_LONG).show(); handler.close(); }
     */

    public void detailSave(View v) {
        String getName = name.getText().toString();
        String getEmail = email.getText().toString();
        String getPhnno = phnno.getText().toString();

        if (getName != null && getEmail != null && getPhnno != null) {
            handler = new DataHandler(getBaseContext());
            handler.open();
            long id = handler.insertData(getName, getEmail, getPhnno);
            Toast.makeText(getBaseContext(), "Data inserted", Toast.LENGTH_LONG).show();
            handler.close();
        }
    }

    public void detailShow(View v) {
        String getName, getEmail, getPhnno;
        getName = "";
        getEmail = "";
        getPhnno = "";
        handler = new DataHandler(getBaseContext());
        handler.open();
        Cursor C = handler.returnData();
        if (C.moveToFirst()) {
            do {
                getName = C.getString(0);
                getEmail = C.getString(1);
                getPhnno = C.getString(2);

            } while (C.moveToNext());
        }
        handler.close();
        Toast.makeText(getBaseContext(), "Name :" + getName + "\n Email :" + getEmail + "\n Contact No :" + getPhnno,
                Toast.LENGTH_LONG).show();
    }
}

Logcat :

09-01 02:53:44.068: D/dalvikvm(1162): GC_FOR_ALLOC freed 142K, 7% free 3231K/3444K, paused 61ms, total 73ms 09-01 02:53:49.408: D/AndroidRuntime(1162): Shutting down VM 09-01 02:53:49.408: W/dalvikvm(1162): threadid=1: thread exiting with uncaught exception (group=0xb1a68ba8) 09-01 02:53:49.458: E/AndroidRuntime(1162): FATAL EXCEPTION: main 09-01 02:53:49.458: E/AndroidRuntime(1162): Process: com.example.wedding_app, PID: 1162 09-01 02:53:49.458: E/AndroidRuntime(1162): java.lang.IllegalStateException: Could not execute method of the activity 09-01 02:53:49.458: E/AndroidRuntime(1162): at android.view.View$1.onClick(View.java:3823) 09-01 02:53:49.458: E/AndroidRuntime(1162): at android.view.View.performClick(View.java:4438) 09-01 02:53:49.458: E/AndroidRuntime(1162): at android.view.View$PerformClick.run(View.java:18422) 09-01 02:53:49.458: E/AndroidRuntime(1162): at android.os.Handler.handleCallback(Handler.java:733) 09-01 02:53:49.458: E/AndroidRuntime(1162): at android.os.Handler.dispatchMessage(Handler.java:95) 09-01 02:53:49.458: E/AndroidRuntime(1162): at android.os.Looper.loop(Looper.java:136) 09-01 02:53:49.458: E/AndroidRuntime(1162): at android.app.ActivityThread.main(ActivityThread.java:5017) 09-01 02:53:49.458: E/AndroidRuntime(1162): at java.lang.reflect.Method.invokeNative(Native Method) 09-01 02:53:49.458: E/AndroidRuntime(1162): at java.lang.reflect.Method.invoke(Method.java:515) 09-01 02:53:49.458: E/AndroidRuntime(1162): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 09-01 02:53:49.458: E/AndroidRuntime(1162): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 09-01 02:53:49.458: E/AndroidRuntime(1162): at dalvik.system.NativeStart.main(Native Method) 09-01 02:53:49.458: E/AndroidRuntime(1162): Caused by: java.lang.reflect.InvocationTargetException 09-01 02:53:49.458: E/AndroidRuntime(1162): at java.lang.reflect.Method.invokeNative(Native Method) 09-01 02:53:49.458: E/AndroidRuntime(1162): at java.lang.reflect.Method.invoke(Method.java:515) 09-01 02:53:49.458: E/AndroidRuntime(1162): at android.view.View$1.onClick(View.java:3818) 09-01 02:53:49.458: E/AndroidRuntime(1162): ... 11 more 09-01 02:53:49.458: E/AndroidRuntime(1162): Caused by: java.lang.NullPointerException 09-01 02:53:49.458: E/AndroidRuntime(1162): at com.example.wedding_app.Contact_Details.detailSave(Contact_Details.java:56) 09-01 02:53:49.458: E/AndroidRuntime(1162): ... 14 more 09-01 02:53:52.018: I/Process(1162): Sending signal. PID: 1162 SIG: 9 09-01 02:53:55.038: D/gralloc_goldfish(1192): Emulator without GPU emulation detected.

Please help me to solve this.

tijko
  • 7,599
  • 11
  • 44
  • 64

1 Answers1

0

getText() does not return null. So it got to be name which is null here. Double check how you are initializing it name = (EditText) findViewById(R.id.c_name);. May be R.id.c_name is to blame.

Ε Г И І И О
  • 11,199
  • 1
  • 48
  • 63