I have value inside my code:
val mainFileName = "Untitled list"
As I know the best way to handle strings is using string resource, so inside String resource file I created this string:
<string name="untitled_list">Untitled list</string>
In order to test if i done everything right, I tested that string on toast and everything is working fine:
Toast.makeText(this, R.string.untitled_list,Toast.LENGTH_SHORT).show()
Then I tried to assign to "mainFileName" that string resource by doing this (inside MainActivity class but outside of method OnCreate):
val previewOfNewListTitle = resources.getString(R.string.untitled_list)
but I get an error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:91)
at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:127)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:121)
at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:543)
at lt.tetro.myapplication.MainActivity.<init>(MainActivity.kt:121)
How can I assign string resource text to "mainFileName" inside of MainActivity class?