2024-01-03 10:32:04.384 4305-4305/com.sevenlearn.mvvmproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sevenlearn.mvvmproject, PID: 4305
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sevenlearn.mvvmproject/com.sevenlearn.mvvmproject.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'io.reactivex.Single com.sevenlearn.mvvmproject.ApiService.getStudents()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'io.reactivex.Single com.sevenlearn.mvvmproject.ApiService.getStudents()' on a null object reference
at com.sevenlearn.mvvmproject.StudentRepository.refreshStudents(StudentRepository.java:20)
at com.sevenlearn.mvvmproject.MainVewModel.<init>(MainVewModel.java:23)
at com.sevenlearn.mvvmproject.MainViewModelFactory.create(MainViewModelFactory.java:22)
at androidx.lifecycle.ViewModelProvider$Factory.create(ViewModelProvider.kt:83)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:187)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:153)
at com.sevenlearn.mvvmproject.MainActivity.onCreate(MainActivity.java:15)
at android.app.Activity.performCreate(Activity.java:6739)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2619)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
2024-01-03 10:32:06.261 4305-4322/com.sevenlearn.mvvmproject D/LeakCanary: Setting up flushing for Thread[LeakCanary-Heap-Dump,5,main]
---------------------
package com.sevenlearn.mvvmproject;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MainVewModel mainVewModel= new ViewModelProvider(this,new MainViewModelFactory(new StudentRepository(ApiServiceProvider.getApiService(),AppDatabase.getInstance(getApplicationContext()).studentDao()))).get(MainVewModel.class);
mainVewModel.getStudents().observe(this,students->{
Log.i("mainactivity", "onCreate: ");
});
mainVewModel.getError().observe(this,error->{
Log.e("mainactivity", "Error: "+error );
});
}
}
----------------------
package com.sevenlearn.mvvmproject;
import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainViewModelFactory implements ViewModelProvider.Factory {
private StudentRepository repository;
public MainViewModelFactory(StudentRepository repository){
this.repository = repository;
}
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
return (T) new MainVewModel(repository);
}
}
--------------------
package com.sevenlearn.mvvmproject;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import java.util.List;
import io.reactivex.CompletableObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
public class MainVewModel extends ViewModel {
private ApiService apiService;
private MutableLiveData<String> error=new MutableLiveData<>();
private StudentRepository repository;
private Disposable disposable;
public MainVewModel(StudentRepository repository){
this.repository = repository;
repository.refreshStudents()
.subscribeOn(Schedulers.io())
.subscribe(new CompletableObserver() {
@Override
public void onSubscribe(Disposable d) {
disposable=d;
}
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
error.setValue("خطای نامشخص");
}
});
}
public LiveData<List<Student>> getStudents() {
return repository.getStudent();
}
public LiveData<String> getError() {
return error;
}
@Override
protected void onCleared() {
disposable.dispose();
super.onCleared();
}
}
---------------------
package com.sevenlearn.mvvmproject;
import androidx.lifecycle.LiveData;
import java.util.List;
import io.reactivex.Completable;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class StudentRepository {
private ApiService apiService;
private StudentDao studentDao;
public StudentRepository(ApiService apiService, StudentDao studentDao) {
}
public Completable refreshStudents(){
return apiService.getStudents().doOnSuccess(students -> studentDao.addStudents(students)).ignoreElement();
}
public LiveData<List<Student>> getStudent(){
return studentDao.getStudents();
}
}