darshan
darshan

Reputation: 4569

App Crash with a Null Object Reference

I am using a fragment, the compiler throws no error. But when I press the specified Button for which I created a listener, The App crashes! the logcat refers a null object reference to this part of the code...

private ImageView i;
    private String key = null;
    private ArrayList<String> lst = new ArrayList();

    private OnClickListener lstn = new OnClickListener() {
        public void onClick(View v)
        {
            EditText et1 = (EditText) v.findViewById(R.id.editText1);

            profilname = et1.getText().toString();


            new getPbLink().execute(new String[]{profilname});

        }

    };


    private OnClickListener lstn1 = new OnClickListener() {
        public void onClick(View v) 
        {
            if (pb == null)
            {
                Toast.makeText(getActivity(),"nodp", 0)
                .show();
            } else 

            { 
                SaveImage(pb);
                Toast.makeText(getActivity(), "saved", 0)
                    .show();
            }
        }
    };

What to do? logcat -

    Process: com.nav.drawer, PID: 21833
12-22 20:43:14.696 21833 21833 E   AndroidRuntime                               java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
12-22 20:43:14.696 21833 21833 E   AndroidRuntime                               at com.nav.drawer.HomeFragment$100000001.onClick(HomeFragment.java:61)
12-22 20:43:14.696 21833 21833 E   AndroidRuntime                               at android.view.View.performClick(View.java:4785)
12-22 20:43:14.696 21833 21833 E   AndroidRuntime                               at android.view.View$PerformClick.run(View.java:19884)
12-22 20:43:14.696 21833 21833 E   AndroidRuntime                               at android.os.Handler.handleCallback(Handler.java:739)
12-22 20:43:14.696 21833 21833 E   AndroidRuntime                               at android.os.Handler.dispatchMessage(Handler.java:95)
12-22 20:43:14.696 21833 21833 E   AndroidRuntime                               at android.os.Looper.loop(Looper.java:135)
12-22 20:43:14.696 21833 21833 E   AndroidRuntime                               at android.app.ActivityThread.main(ActivityThread.java:5343)
12-22 20:43:14.696 21833 21833 E   AndroidRuntime                               at java.lang.reflect.Method.invoke(Native Method)
12-22 20:43:14.696 21833 21833 E   AndroidRuntime                               at java.lang.reflect.Method.invoke(Method.java:372)

Upvotes: 0

Views: 273

Answers (1)

Albert
Albert

Reputation: 144

On an "onClick" event, the View passed is the view where you clicked. So, if you are clicking a button, you can't find any EditText because it doesn't exists here.

You need to call the function "findViewById" on the view that contains it, and not on the view passed by the method onClick.

Upvotes: 7

Related Questions