0

I am making an application, in this application I wrote the country names in strings.xml file. I want to get these country names in my Utils class, but I can't get these strings. I would be glad if you help. My code is below:

class Utils {

    private var resources: Resources? = null
    private val utils = resources!!

    fun getAsiaCountriesFlags(): MutableList<Flag> {
        return mutableListOf(
            Flag(
                R.drawable.asia_turkey,
                listOf(
                    utils.getString(R.string.asia_armenia),
                    utils.getString(R.string.asia_iran),
                    utils.getString(R.string.asia_turkey),
                    utils.getString(R.string.asia_azerbaijan)
                ),
                utils.getString(R.string.asia_turkey)
            )
        )
    }
}

private val utils = resources!! It gives me an error on the this line. The error I'm getting is this:

Process: com.example.flagquizapp, PID: 8949
    java.lang.NullPointerException
        at com.example.flagquizapp.util.Utils.<init>(Utils.kt:10)
3
  • You have declared nullable property resources and you are trying to access resources or context from that property while it has no access to it. Your Utils class is not Content or Resources aware. So make use of function getAsiaCountriesFlags( ) to use parameters, like getAsiaCountriesFlags(resources: Resources) { }. Commented Dec 16, 2021 at 8:46
  • resources is null because you never initialized it . Commented Dec 16, 2021 at 8:47
  • How can I initialize it? @ADM Commented Dec 16, 2021 at 8:49

4 Answers 4

2
**Before** 
<string-array name="country_array">

    <item>asia_armenia</item>
    <item>asia_iran</item>
    <item>asia_turkey</item>
    <item>asia_azerbaijan</item>
    <item>asia_turkey</item>

    </string-array>
**Create res--> Array.xml**
**After**
  var categories: Array<String?>
        categories=getResources().getStringArray(R.array.vehiclescategory_array);
Sign up to request clarification or add additional context in comments.

Comments

2

Your resources object is not initialized so you should get it in the class constructor or method

In Class Constructor

class Utils(private val resources: Resources) {

    fun getAsiaCountriesFlags(): MutableList<Flag> {
        ...
    }
}

In Method

class Utils {

    fun getAsiaCountriesFlags(resources: Resources): MutableList<Flag> {
        ...
    }
}

Comments

0
class Utils(private val context: Context) {

// NOT NEEDED
//private var resources: Resources? = null

// Create Utils class by passing context and
// it'll Lazily initialized utils variable on first call/access
private val utils by lazy { context.resources }

fun getAsiaCountriesFlags(): MutableList<Flag> {
    return mutableListOf(
        Flag(
            R.drawable.asia_turkey,
            listOf(
                utils.getString(R.string.asia_armenia),
                utils.getString(R.string.asia_iran),
                utils.getString(R.string.asia_turkey),
                utils.getString(R.string.asia_azerbaijan)
            ),
            utils.getString(R.string.asia_turkey)
        )
    )
}
}

Comments

-1

Consider the following:

private var resources: Resources? = null

private val utils = resources ?: " "

to avoid a NullPointerException. The elvis operator in Kotlin is used to provide a default value when a nullable variable is null. In this case, utils will be initialized to resources if it is not null, and if it is null, it will be initialized to an empty string (" ").

3 Comments

Please add an explanation for it
@RohitGupta to avoid a NullPointerException. The elvis operator in Kotlin is used to provide a default value when a nullable variable is null. In this case, utils will be initialized to resources if it is not null, and if it is null, it will be initialized to an empty string (" ").
Thanks, the explanation is not for me. The down vote is not mine, but the probable reason for it is that the answer needs an explanation. Please edit it and shift the comment there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.