Java – Viewmodel could not create an instance of the Android Java MVVM class

Viewmodel could not create an instance of the Android Java MVVM class… here is a solution to the problem.

Viewmodel could not create an instance of the Android Java MVVM class

I’m trying to implement a movie app that allows browsing movies and checking their ratings… When I tried to implement the MVVM model using my MovieListViewModel, I ran into a problem where the debugger told me that I could not create an instance of this class (MovieListViewModel).

What I did was try to follow some tutorials and look for all the questions about the View model in this site, they usually say I should use the constructor of the View model as a public constructor, or in the constructor (parameters and things like that) but I’m sure I followed the guidelines on constructors

(I tried to follow this tutorial to implement the first part of my application)
https://medium.com/@eladb4382/paging-library-viewmodel-livedata-room-and-retrofit-66bf6a0eef9d
I did basically the same code, but I had the error and he didn’t….

This is my ListView model class:

The public class MovieListViewModel extends AndroidViewModel {

private MovieRepository repository;

public MovieListViewModel(@NonNull Application application) {
    super(application);
    repository = MovieRepository.getInstance(application);
}

public LiveData<PagedList<Movie>> getMovies() {
    return repository.getMovies();
}

public LiveData<NetworkState> getNetworkState() {
    return repository.getNetworkState();
}}

What’s the problem:

The public class MovieListFragment extension Fragment implements OnMovieItemClicked {

private final String TAG = MovieListFragment.class.getSimpleName() ;

protected MovieListViewModel viewmodel ;
private MovieDetailsViewModel movieDetailsViewModel  ;

protected RecyclerView recyclerView ;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.movie_list_fragment , container , false );
    recyclerView = view.findViewById(R.id.moviesRecyclerView);
    recyclerView.setLayoutManager(new GridLayoutManager(getContext(),3));
    Log.d(TAG , "creating viewmodel ..." );
    viewmodel = ViewModelProviders.of(getActivity()).get(MovieListViewModel.class);
    observersRegisters() ;
    return view;
}

private void observersRegisters() {
    final MovieAdapter adapter = new MovieAdapter(this);
    viewmodel.getMovies().observe(this , adapter ::submitList);

viewmodel.getNetworkState().observe(this,networkState ->{
        adapter.setNetworkState(networkState);
    });
    recyclerView.setAdapter(adapter);
    movieDetailsViewModel = ViewModelProviders.of(getActivity()).get(MovieDetailsViewModel.class);
}

(This is not the whole fragment class, it’s just the part where the error occurs).

The activity I call this fragment is here::

The public class MainActivity extends AppCompatActivity {

private static final String TAG = MainActivity.class.getSimpleName() ;

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

if(findViewById(R.id.fragmentsContainer)!= null) {
        if(savedInstanceState != null ){
            return  ;
        }
        MovieListFragment  listFragment = new MovieListFragment() ;
        listFragment.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction().add(R.id.fragmentsContainer,listFragment).commit();
    }}

I also uploaded the entire project to GitHub in case anyone wanted to check this issue :

https://github.com/TheDeathLorD/MovieApp

This is the error I got at runtime :

E/AndroidRuntime: Fatal Exception: main
Process: com.example.movieapp2, PID: 4935
java.lang.RuntimeException: Unable to create an instance of class com.example.movieapp2.ui.viewmodel.MovieListViewModel
In android.arch.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:207)
In android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134)
In android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102)
In com.example.movieapp2.ui.view.MovieListFragment.onCreateView(MovieListFragment.java:40).

Solution

In your case, you

should see the whole log, I tried your code, and there is more information below the error you posted :

     Caused by: java.lang.IllegalArgumentException: baseUrl must end in /: http://api.themoviedb.org/3/movie/popular
        at retrofit2. Retrofit$Builder.baseUrl(Retrofit.java:515)
        at retrofit2. Retrofit$Builder.baseUrl(Retrofit.java:458)
        at com.example.movieapp2.repository.network.api.MovieAPIClient.getInstance(MovieAPIClient.java:27)
        at com.example.movieapp2.repository.network.paging.NetMoviePageKeyedDataSource.<init>(NetMoviePageKeyedDataSource.java:32)
        at com.example.movieapp2.repository.network.paging.NetMovieDataSourceFactory.<init>(NetMovieDataSourceFactory.java:18)
        at com.example.movieapp2.repository.MovieRepository.<init>(MovieRepository.java:27)
        at com.example.movieapp2.repository.MovieRepository.getInstance(MovieRepository.java:56)

This means that you should add a / after your baseUrl

Then you’ll see another crash prompt Missing either @GET URL or @Url parameter. This is because @GET URL is blank in MovieAPIInterface.getMovies, and I suggest you change baseUrl to http://api.themoviedb.org/3/movie and pop to @GET annotations

Related Problems and Solutions