Mike
Mike

Reputation: 6839

Edit text toString throwing null pointer

I have a fragment which launches a dialog. In the dialog there is a text view, and an button. When you press the button inside the dialog it should get the value from the text field and send it to an async task.

My app is force closing when I try and retrieve the vlaue of the text field:

public class ListPage extends Fragment {

    String beerID = "";
    String userID = "";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        //set layout here
        View v = inflater.inflate(R.layout.frag_listpage, container, false);
        setHasOptionsMenu(true);
        getActivity().setTitle("Your Lists");

        //get user information
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String userName = prefs.getString("userName", null);
        userID = prefs.getString("userID", null);
        beerID = prefs.getString("beerID", null);




        //todo: get lists and add checkboxes with onclick



        Button bt2 = (Button)v.findViewById(R.id.buttonAddList);

        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                // Create custom dialog object
                final Dialog dialog = new Dialog(getActivity());
                // Include dialog.xml file
                dialog.setContentView(R.layout.listdialog);
                // Set dialog title
                dialog.setTitle("Create a List");

                // set values for custom dialog components - text, image and button
                TextView text = (TextView) dialog.findViewById(R.id.textDialog);


                dialog.show();

                Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
                // if decline button is clicked, close the custom dialog
                declineButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        //todo: run async task to save text field value as a list

                        //get value from text field
                        EditText mEdit   = (EditText)v.findViewById(R.id.listName);

                        String lName = mEdit.getText().toString();
                        try {
                            lName = URLEncoder.encode(lName, "UTF-8");
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }


                        //run async task with beerID, userID and lName
                        //log in url
                        String url = "myURL;
                        Log.d("registerURL", url);
                        //async task for getting json
                        new AddList(getActivity()).execute(url);


                        // Close dialog
                        dialog.dismiss();
                    }
                });


            }

        });




        // Inflate the layout for this fragment
        return v;

    }

The error is:

03-26 16:06:19.340    7659-7659/com.beerportfolio.beerportfoliopro E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.beerportfolio.beerportfoliopro.ListPage$1$1.onClick(ListPage.java:87)
            at android.view.View.performClick(View.java:4280)
            at android.view.View$PerformClick.run(View.java:17984)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5789)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
            at dalvik.system.NativeStart.main(Native Method)

Line 87 is:

String lName = mEdit.getText().toString();

Upvotes: 0

Views: 267

Answers (1)

Szymon
Szymon

Reputation: 43023

You're finding your EditText in a wrong way. Replace

EditText mEdit   = (EditText)v.findViewById(R.id.listName);

with

EditText mEdit   = (EditText)dialog.findViewById(R.id.listName);

v is the button that was clicked, so it cannot find the EditText inside itself.

Upvotes: 1

Related Questions