I'm building an Android app that processes saved Wi-Fi information. I have a function that returns an ArrayList of WifiEntry objects, but will return null if it can't access the Wi-Fi configuration file (for example, if root access isn't available). At the moment, I'm dealing with it like this:
wifiEntries = try {
WifiEntryLoader().readOreoFile()
} catch (e: IllegalStateException) {
// Important irelevant stuff
ArrayList()
}
My question is: is there a "better" way to deal with the possibility of a null than by catching an IllegalStateException? I don't want to call my loader function twice. As far as I know, I can't use the Elvis operator to run code, unless I use it with an if expression (ugly and hacky)
Based on the elaboration in the comment, it looks like you should just use the Elvis operator and
emptyList: