-2

Here is the LOG:

E/AndroidRuntime: FATAL EXCEPTION: main
                      Process: com.example.chong.medicinereminder, PID: 3221
                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chong.medicinereminder/com.example.chong.medicinereminder.AddMedicine}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                          at android.app.ActivityThread.-wrap12(ActivityThread.java)
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                          at android.os.Handler.dispatchMessage(Handler.java:102)
                          at android.os.Looper.loop(Looper.java:154)
                          at android.app.ActivityThread.main(ActivityThread.java:6077)
                          at java.lang.reflect.Method.invoke(Native Method)
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                          at com.example.chong.medicinereminder.AddMedicine.onCreate(AddMedicine.java:36)
                          at android.app.Activity.performCreate(Activity.java:6664)
                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                          at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                          at android.os.Looper.loop(Looper.java:154) 
                          at android.app.ActivityThread.main(ActivityThread.java:6077) 
                          at java.lang.reflect.Method.invoke(Native Method)

 

I really need help to solve this problem because all my syntax seem to have no error at all please help I am doing a Medicine Reminder App and the app got crashed everytime i pressed the 'AddMedicine' button. Below is the coding of problem interface:

public class AddMedicine extends AppCompatActivity {
    EditText med_name, med_reason, med_dosage;
    List<MedicineList> MedicineList = new ArrayList<MedicineList>();


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

        med_name = (EditText) findViewById(R.id.text_med_name);
        med_reason = (EditText) findViewById(R.id.text_med_reason);
        med_dosage = (EditText) findViewById(R.id.text_med_dosage);

        final Button button_save = (Button) findViewById(R.id.button_save);
        button_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveMedicine(med_name.getText().toString(), med_reason.getText().toString(), med_dosage.getText().toString());
                Toast.makeText(getApplicationContext(), med_name.getText().toString() + " has been added to the list", Toast.LENGTH_SHORT).show();
            }

        });

        med_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                button_save.setEnabled(!med_name.getText().toString().trim().isEmpty());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void saveMedicine(String name, String reason, String dosage) {
        MedicineList.add(new MedicineList(name, reason, dosage));
    }

    private class MedicineListAdapter extends ArrayAdapter<MedicineList> {


        public MedicineListAdapter() {
            super(AddMedicine.this, R.layout.medicine_list, MedicineList);
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            if (view == null)
                view = getLayoutInflater().inflate(R.layout.medicine_list, parent, false);
            MedicineList currentMedicine = MedicineList.get(position);

            TextView name = (TextView) view.findViewById(R.id.medicine_name);
            name.setText(currentMedicine.getName());

            TextView reason = (TextView) view.findViewById(R.id.medicine_reason);
            reason.setText(currentMedicine.getReason());

            TextView dosage = (TextView) view.findViewById(R.id.medicine_dosage);
            dosage.setText(currentMedicine.getDosage());

            return view;
        }


    }


    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_medicine_add, menu);
        return true;
    }

}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

1

It looks like an Button element with id of button_save is missing in your R.layout.activity_add_medicine xml file.

Because findViewById() returns element for given id or null, if element with such id wasn't found.