-2

i try to add onClick this button then create contact in database

Here is code i try to add it to main_fragment

  final Button addBtn = (Button) view.findViewById(R.id.btnadd);
    addBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Uri imageUri = Uri.parse("android.resource://org.intracode.contactmanager/drawable/no_user_logo.png");
            import_fragment.Contact contact = new import_fragment.Contact(dbHandler.getContactsCount(), String.valueOf(nametxt.getText()), String.valueOf(phoneTxt.getText()), String.valueOf(emailTxt.getText()), String.valueOf(addressTxt.getText()), imageUri);
            if (!contactExists(contact)) {
                dbHandler.createContact(contact);
                Contacts.add(contact);
                contactAdapter.notifyDataSetChanged(); // Error in this line
                Toast.makeText(getActivity().getApplicationContext(), String.valueOf(nametxt.getText()) + " has been added to your Contacts!", Toast.LENGTH_SHORT).show();
                return;
            }
            Toast.makeText(getActivity().getApplicationContext(), String.valueOf(nametxt.getText()) + " already exists. Please use a different name.", Toast.LENGTH_SHORT).show();
        }
    });

When i press this button in my app, 'app has stopped working'

Here is my logcat

01-22 08:31:04.014 29398-29398/com.al3almya.users.al3almya E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.al3almya.users.al3almya, PID: 29398
                                                                         java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ArrayAdapter.notifyDataSetChanged()' on a null object reference
                                                                             at com.al3almya.users.al3almya.main_fragment$1.onClick(main_fragment.java:77)
                                                                             at android.view.View.performClick(View.java:4848)
                                                                             at android.view.View$PerformClick.run(View.java:20262)
                                                                             at android.os.Handler.handleCallback(Handler.java:815)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                             at android.os.Looper.loop(Looper.java:194)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5637)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at java.lang.reflect.Method.invoke(Method.java:372)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
  • I don't know the root cause.. try to do null check if(contactAdapter != null)contactAdapter.notifyDataSetChanged(); post more code where u r initialising the adapter. – Raghavendra Jan 22 '16 at 06:36
  • 1
    Now a days SO is full of negative votes, LOL :) – Gunaseelan Jan 22 '16 at 06:37
  • 1
    you have not initialized `contactAdapter`, it's null – Ravi Jan 22 '16 at 06:37
  • Raghavendra, button is work without app stop now but when i try to add exists contact its not say already exists. Please use a different name.' and i think when i add contact its not add to my database because its doesn't appear any contact from my database in the other page – Maystro JeKa Jan 22 '16 at 06:50

2 Answers2

0

It appears that the variable contactAdapter has not been set.

Ken Clubok
  • 1,238
  • 6
  • 11
0

Either the contactAdapter is not initialized(contactAdater is null) or the object on which the adapter is set is null.

Make sure you have initialized all the variables. Else try running dubugger.

kishorepatel
  • 139
  • 1
  • 6