Reputation: 285
I'm currently learning Dagger Hilt but I've come across an issue:
kotlin.UninitializedPropertyAccessException: lateinit property str has not been initialized
at com.example.kotlin20test.MainActivity.getStr(MainActivity.kt:24)
at com.example.kotlin20test.MainActivity.onCreate(MainActivity.kt:34)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
Here are my files:
MainActivity:
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject lateinit var str: String
@Inject private lateinit var api: JsonPlaceholderApi
lateinit var mainBinding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mainBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(R.layout.activity_main)
if(str != null){
println(str)
}
}}
MyModule:
@Module
@InstallIn(SingletonComponent::class)
object MyModule{
@Provides
fun provideString(): String{
return "Look, It's a string"
}
}
And I've also tried Retrofit with hilt and it gave me the same error, and I've read many Stack overflow regarding this issue and tried the answers but nothing seems to work.
Edit: I forgot to add the app class and the manifest so here it is:
MyApp
@HiltAndroidApp
class MyApp : Application(){
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kotlin20test">
<application
android:name=".MyApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Kotlin20Test">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Thanks in advance.
Upvotes: 1
Views: 1330
Reputation: 563
hey you forget the @Singleton
annotation
@Module
@InstallIn(SingletonComponent::class)
object MyModule{
@Singleton <------ include this
@Provides
fun provideString(): String{
return "Look, It's a string"
}
}
Upvotes: -1
Reputation: 21
The proper way to check if a Kotlin lateinit var has been initialized is with the KProperty value isInitialized, turning this:
if(str != null){
println(str)
}
into this:
if(::str.isInitialized){
println(str)
}
Edit: As another user pointed out I erroneously included a ! originally.
Upvotes: 2
Reputation: 1220
While you have not yet initialized your variable, it will give UninitializedPropertyAccessException when you try to get it, even for str != null
as @VelocityCubeR said, kotlin have other method to check if a property has been initialized, buе he was wrong with !
you must change str != null
to ::str.isInitialized
Dagger Hilt not relevant to this exception, this is basic kotlin syntax, read more here
Upvotes: 0