Alex Rodionow
Alex Rodionow

Reputation: 257

Dagger Hilt. Runtime error. Error: ViewModel has no zero argument constructor (kotlin)

I get an error (MainViewModel has no zero argument constructor). It seems to me that mistake is in misusing Hilt, but I can't find. There are similar questions on SA, but they don't fit my case. I can't find where did I go wrong and will be gratifeul for any help.

Error:

java.lang.RuntimeException: Cannot create an instance of class mypackage.main.MainViewModel
  /* bla bla bla */
Caused by: java.lang.InstantiationException: java.lang.Class<mypackage.main.MainViewModel> has no zero argument constructor
 at java.lang.Class.newInstance(Native Method)
 at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)
 ... 39 more

ViewModel begins like that:

@HiltViewModel
class MainViewModel @Inject constructor(
    private val repo: MainRepository,
    private val dispatchers: DispatcherProvider
) : ViewModel() {
 
    // body
}

In MainActivity:

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    private val viewModel: MainViewModel by viewModels()
// etc

AppModule:

@Module
@InstallIn(SingletonComponent::class)
object AppModule {

    @Singleton
    @Provides
    fun provideCurrencyApi() : CurrencyApi = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
        .create(CurrencyApi::class.java)

    @Singleton
    @Provides
    fun provideMainRepository(api: CurrencyApi): MainRepository = DefaultMainRepository(api)

    @Singleton
    @Provides
    fun provideDispatchers(): // blablabla
    }
}

MainRepository:

interface MainRepository {
    suspend fun getRates(base: String) : Resource<CurrencyResponse>
    }

DefaultMainRepository

class DefaultMainRepository @Inject constructor(
    private val api: CurrencyApi
) : MainRepository {

    override suspend fun getRates(base: String): Resource<CurrencyResponse> {
        return try {
            val response = api.getRates(base)
            val result = response.body()
            if (response.isSuccessful && result != null) {
                Resource.Success(result)
            } else {
                Resource.Error(response.message())
            }
        } catch (e: Exception) {
            Resource.Error(e.message ?: "An error occurred")
        }
    }
}

Upvotes: 2

Views: 1481

Answers (1)

Alex Rodionow
Alex Rodionow

Reputation: 257

I solved this problem by changing Dagger Hilt dependencies versions to earlier. I think there was mismatch in those versions. The rest of the code turned out to be correct, it seems..

Upvotes: 3

Related Questions