-1

I am just trying to setOnClickListener to a method on a an image button and it leads to null pointer exception error.

Logcat shown below:

02-25 10:49:43.051 13936-13936/com.example.l33902.contactmanagment1512 E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.example.l33902.contactmanagment1512, PID: 13936
   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.l33902.contactmanagment1512/com.example.l33902.contactmanagment.EditActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object referenceat android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3155)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3263)
   at android.app.ActivityThread.access$1000(ActivityThread.java:197)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1687)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:145)
   at android.app.ActivityThread.main(ActivityThread.java:6897)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
   at com.example.l33902.contactmanagment.EditActivity.onCreate(EditActivity.java:158)
   at android.app.Activity.performCreate(Activity.java:6550)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3108)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3263) 
   at android.app.ActivityThread.access$1000(ActivityThread.java:197) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1687) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:145) 
   at android.app.ActivityThread.main(ActivityThread.java:6897) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:372) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 

Below are my relevant codes:

ImageButton saveButton;

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_activity);

saveButton = (ImageButton) findViewById(R.id.edit_activity_save);
 displayNameEditText = (EditText) findViewById(R.id.edit_activity_text_cal_name);
    displayNameEditTextLyout = (TextInputLayout) findViewById(R.id.edit_activity_text_cal_name_layout);

 saveButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                save();
            }
        });

}

 private void save() {
        if (displayNameEditText.getText().length() == 0) {
            displayNameEditTextLyout.setError(getString(R.string.edit_activity_error_empty_name));
        } else {
            displayNameEditTextLyout.setError(null);
            if (edit)
                updateCalendar();
            else
                addCalendar(EditActivity.this);
        }
    }

private void updateCalendar() {
        CalendarController.updateCalendar(mCalendarId, displayNameEditText.getText()
                .toString(), colorPicker.getColor(), getContentResolver());
        finish();
    }

In my layout xml:

<ImageButton
                    android:id="@+id/edit_activity_save"
                    android:layout_width="64dp"
                    android:layout_height="64dp"
                    android:contentDescription="@string/edit_activity_save"
                    android:src="@drawable/ic_save_white_24dp"/>

I cannot figure what is the error about as I seem to have declared and point everything to the correct method.

Donovan Tan
  • 326
  • 1
  • 5
  • 16

3 Answers3

0

In your code the displayNameEditText is not initialized, you can first initialize the view then use then.

Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68
0

You need to check the identity of Image buttin in the xml layout.

saveButton = (ImageButton) findViewById(R.id.edit_activity_save);

Perhaps you seems to used wrong identity.

See android document for more detail.

http://developer.android.com/reference/android/app/Activity.html#findViewById(int)

findViewById function returns null if no found view.

Enjoy good coding!

covernal
  • 329
  • 1
  • 3
  • 20
0

Try this corrected code:

ImageButton saveButton;
EditText displayNameEditText //declare it first
EditText displayNameEditTextLyout //if it's an EditText


@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_activity);

saveButton = (ImageButton) findViewById(R.id.edit_activity_save);
//add this line
displayNameEditText = (EditText) findViewById(R.id.your_particular_xml_id);  //your_particular_xml_id must exist in your xml file
//add this line
displayNameEditTextLyout = (EditText) findViewById(R.id.particular_xml_id);  // particular_xml_id must exist in your xml file

saveButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                save();
            }
        });

}

 private void save() {
        if (displayNameEditText.getText().length() == 0) {
            displayNameEditTextLyout.setError(getString(R.string.edit_activity_error_empty_name));
        } else {
            displayNameEditTextLyout.setError(null);
            if (edit)
                updateCalendar();
            else
                addCalendar(EditActivity.this);
        }
    }

private void updateCalendar() {
        CalendarController.updateCalendar(mCalendarId, displayNameEditText.getText()
                .toString(), colorPicker.getColor(), getContentResolver());
        finish();
    }

I am assuming you have also defined addCalendar() method in your code.If not, you have to because you have used it in save() method.

I hope it helps.

Nike15
  • 544
  • 3
  • 16