简体   繁体   中英

Why am I getting EditText.getText()' on a null object reference?

I'm building an app that displays an alert dialog at FAB click, takes the user input and names an ExpandableListView group header with that user input. When I click the FAB button and put in the text, it gives me a null object reference.

I've initialized my semester before the onCreate()

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    ExpandableListView expandableListView;
    ExpandableListViewAdapter adapter;
    private FloatingActionButton addItems;
    private String semester = "";

In my addItems() method, which is called when there is a FAB click, I am creating my custom Alert Dialog, getting the user input String and placing it in my ArrayList in the adapter.

public void addItems() {
        final EditText et_semester = (EditText)findViewById(R.id.et_semester);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Create A Semester");
        builder.setView(R.layout.custom_layout);
        builder.setCancelable(false);
        builder.setPositiveButton("DONE", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                semester = et_semester.getText().toString();
                adapter.groupNames.add(semester);
            }
        });

        builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        adapter.notifyDataSetChanged();
        builder.show();
    }

But when I click the DONE button on the AlertDialog, this is my stacktrace:

05-30 05:35:10.295 20943-20943/myapp.onur.expandablelistviewexample E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      Process: myapp.onur.expandablelistviewexample, PID: 20943
                                                                                      java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                                                                                          at myapp.onur.expandablelistviewexample.MainActivity$1.onClick(MainActivity.java:45)

EDIT This is my MainActivity :

public class MainActivity extends AppCompatActivity {

    ExpandableListView expandableListView;
    ExpandableListViewAdapter adapter;
    private FloatingActionButton addItems;
    private String semester;

    int counter;

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

        expandableListView = (ExpandableListView) findViewById(R.id.expListView);

        adapter = new ExpandableListViewAdapter(MainActivity.this);
        expandableListView.setAdapter(adapter);
        addItems = (FloatingActionButton) findViewById(R.id.add_items);

        final EditText et_semester = (EditText)findViewById(R.id.et_semester);
    }

    public void addSemester(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Create A Semester");
        builder.setView(R.layout.custom_layout);
        builder.setCancelable(false);

        builder.setPositiveButton("DONE", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                final EditText et_semester = (EditText).findViewById(R.id.et_semester);
                semester = etSemester.getText().toString();
                adapter.groupNames.add(semester);
            }
        });

        builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        adapter.notifyDataSetChanged();
        builder.show();
    }

}

Your EditText R.id.et_semester is not in your activity content view, So you must find this view directing in your dialog view

    // Inflate your view
    View dialogView = LayoutInflater.from(context).inflate(R.layout.custom_layout, null);
    // Bind views
    final EditText et_semester = (EditText) dialogView.findViewById(R.id.et_semester);
    // Create dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(context)
            .setTitle("Create A Semester")
            .setView(dialogView)
            .setCancelable(false);

Hank Moody's answer was correct. This is what I did.

public void addSemester(View v){

        View dialogView = LayoutInflater.from(MainActivity.this).inflate(R.layout.custom_layout, null);
        final EditText et_semester = (EditText)dialogView.findViewById(R.id.et_semester);

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Create A Semester");
        builder.setView(dialogView);
        builder.setCancelable(false);

        builder.setPositiveButton("DONE", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                semester = et_semester.getText().toString();
                adapter.groupNames.add(semester);
            }
        });

        builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        adapter.notifyDataSetChanged();
        builder.show();
    }

Calling the method addSemester() in onCreate() is not a good idea because there are certain settings in the method that are triggered through the FAB click but if you place it in the onCreate() method, they run the moment the activity is created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM