My app in android studio is crashing due to ContactViewModell class . It is an list of contacts app with CRUD operations

17 Views Asked by At

When I comment out only this part of my code my app works fine but when it is the part of the code then app crashes

     contactViewModel.getAllContacts().observe(this, contacts -> {
         

        });

Here is the code above it:

    ContactViewModel contactViewModel = new ViewModelProvider.AndroidViewModelFactory(MainActivity.this
                .getApplication())
                .create(ContactViewModel.class);

Here is the ContactViewModel class:

    public class ContactViewModel extends AndroidViewModel {

    public static ContactRepository repository;
    public final LiveData<List<Contact>> allContacts;


    public ContactViewModel(@NonNull Application application) {
        super(application);
        repository = new ContactRepository(application);
        allContacts = repository.getAllData();
    }

    public LiveData<List<Contact>> getAllContacts() { return allContacts; }
    public static void insert(Contact contact) { repository.insert(contact); }

    public LiveData<Contact> get(int id) { return repository.get(id);}
    public static void update(Contact contact) { repository.update(contact);}
    public static void delete(Contact contact) { repository.delete(contact);}
}


I was expecting my code running fine. Then I just totally copied the whole code from the tutorial from which I am learning this but it had the same bug
Here are my dependencies:

     dependencies {



    implementation (libs.appcompat)

    // Dependencies for working with Architecture components
    // You'll probably have to update the version numbers in build.gradle (Project)

    // Room components
    implementation (libs.room.runtime)
    implementation(libs.activity)
    annotationProcessor (libs.room.compiler)
    androidTestImplementation (libs.room.testing)

    // Lifecycle components
    implementation (libs.lifecycle.viewmodel)
    implementation (libs.lifecycle.livedata)
    implementation (libs.lifecycle.common.java8)

    // UI
    implementation (libs.constraintlayout)
    implementation (libs.material)

    // Testing
    testImplementation (libs.junit)
    androidTestImplementation (libs.core.testing)
    androidTestImplementation ("androidx.test.espresso:espresso-core:3.4.0") {
        exclude ("com.android.support", "support-annotations")
    }
    androidTestImplementation (libs.ext.junit)



}   
0

There are 0 best solutions below