K
K
Kostya Bakay2015-07-29 02:47:32
Android
Kostya Bakay, 2015-07-29 02:47:32

Why does a NullPointerException appear when clicking on a button in a dialog box?

In the application, I use a FloatingActionButton and after clicking it, a dialog box opens where I will enter data. The click handler for the FloatingActionButton works fine, but the handler for another button in the dialog throws a NullPointerException. The dialog box itself opens normally with input fields, but even before the button is clicked, the catch block is processed in the dialog box. Why am I getting this exception? For clarity, here is the code for the onCreate method.

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

    subjectsList = new ArrayList<Subjects>();
    listView = (ListView) findViewById(R.id.listView);
    adapter = new ArrayAdapter<Subjects>(this, android.R.layout.simple_list_item_1, subjectsList);
    listView.setAdapter(adapter);

    addSubjectButton = (FloatingActionButton) findViewById(R.id.addSubjectButton);
    addSubjectDialogButton = (Button) findViewById(R.id.addSubjectDialogButton);

    // Создание диалога для ввода данных про предмет
    dialog = new Dialog(MainActivity.this);
    dialog.setTitle("Додавання предмету"); 
    dialog.setContentView(R.layout.subject_dialog); 

    TextView enterSubjectInfo = (TextView) dialog.findViewById(R.id.enterSubjectInfoView);
    enterSubjectInfo.setText("Назва предмету");

    final EditText enterSubject = (EditText) dialog.findViewById(R.id.enterSubjectView);

    TextView enterCoefficientInfo = (TextView) dialog.findViewById(R.id.enterCoefficientInfoView);
    enterCoefficientInfo.setText("Коефіцієнт");

    final EditText enterCoefficient = (EditText) dialog.findViewById(R.id.enterCoefficientView);

    TextView enterGradeInfo = (TextView) dialog.findViewById(R.id.enterGradeInfoView);
    enterGradeInfo.setText("Оцінка");

    final EditText enterGrade = (EditText) dialog.findViewById(R.id.enterGradeView);

    addSubjectButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.show(); // Выводим диалоговое окно на экран

            // NULL POINTER EXCEPTION!!!!!!
            try {
                addSubjectDialogButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //Subjects sub = new Subjects(enterSubject.getText().toString(), Double.parseDouble(enterCoefficient.getText().toString()), Double.parseDouble(enterGrade.getText().toString()));
                        //sub.toString();
                        //adapter.add(sub);
                        enterSubject.setText("good");
                    }
                });
            } catch (NullPointerException ex) {
                enterSubject.setText("bad");
            }
        }
    });
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Emin, 2015-07-29
@kostyabakay

Come on, what do you expect from this code?

addSubjectDialogButton = (Button) findViewById(R.id.addSubjectDialogButton);

You need to understand that since a nalpointer means a pointer to addSubjectDialogButtonzero, which means that at the time of searching for a button with such an id, it simply was not in the activation markup. It is understandable, this button is in the markup of the dialog.
In general, it was worth reading off the docks first.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question