kotlin - 为什么在获取 JSON 时会出现 NullPointerException?
问题描述
我不断收到 NullPointerException return place
。
当我调试应用程序时,代码会跳过onFailure()
andonResponse()
方法。
以前,这可行,但我将其重构为当前类。
class Repository private constructor() {
private val baseUrl: String = "http://api.openweathermap.org/"
val client = OkHttpClient.Builder()
.addInterceptor(HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY))
.build()
val retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(MoshiConverterFactory.create())
.client(client)
.build()
val networkApi = retrofit.create(NetworkApi::class.java)
private object Holder { val INSTANCE = Repository() }
companion object {
val instance: Repository by lazy { Holder.INSTANCE }
}
fun fetchWeatherData(placeName: String): Place {
var place: Place? = null
val call: Call<Place> = networkApi.getPlaceWeather(placeName)
call.enqueue(object : Callback<Place> {
override fun onFailure(call: Call<Place>?, t: Throwable?) {
println(t?.message)
}
override fun onResponse(call: Call<Place>?, response: Response<Place>?) {
if (response != null && response.isSuccessful && response.body() != null) {
place = response.body() as Place
println(place.toString())
}
}
})
return place!!
}
}
class MainPresenter(private val view: MainContract.View, val context: Context) : MainContract.Presenter {
val repository = Repository.instance
...
override fun updateListOfPlaces() {
var places = mutableListOf<Place>()
for (index in 0 until favPlaceStrings.size) {
places.add(repository.fetchWeatherData(favPlaceStrings.elementAt(index)))
}
view.showFavouritePlaces(places)
}
}
解决方案
您使用改造的方式使其具有异步行为,这意味着其中的代码onFailure
可能onResponse
会在您有机会从fetchWeatherData
. 换句话说,你不能假设place
当你返回时它会有一个值fetchWeatherData
,这实际上是正在发生的事情,place
仍然是null
并且调用!!
将导致你遇到的空指针异常。
要解决此问题,您可以将使用改造的方式更改为同步,或者使用回调之类的方法。
就个人而言,我更喜欢回调方法/反应流,你可以在这里查看。
使代码同步很可能会导致其他问题,例如主线程上的网络调用,这是不允许的并导致应用程序崩溃。
推荐阅读
- mysql - 如何在node.js中实现一个promise代码片段代替Mysql客户端
- python - 如何将 Kivy 画布转换为 SVG?
- regex - Regex 正则表达式最小字符数
- solr - 如何在 solr schema.xml 中读取 DECIMAL 数据类型:audit_internal_id decimal(38,10)
- python-3.x - Alexa python lambda 无法导入请求
- mysql - Laravel 中的批量更新
- java - 如何使用代码模型调用另一个类的方法
- sympy - SymPy 可以识别产品的衍生物吗?
- docker - docker swarm,服务任务未部署到新创建的 swarm 工作节点
- m4 - 命令行上的 m4 定义