panpf / sketch

Sketch is an image loading library designed for Compose Multiplatform and Android View. It is powerful and rich in functions. In addition to basic functions, it also supports GIF, SVG, video thumbnails, Exif Orientation, etc.
Apache License 2.0
2k stars 307 forks source link

加载图片出现 java.lang.NullPointerException: getHeaderField(...) must not be null #193

Closed lee6256 closed 4 months ago

lee6256 commented 4 months ago

Describe the bug

我这边加载一张图片失败了,从错误堆栈中找到了 Sketch 加载库的源码位置,源码中有读取 Response 中的 Content-Length 字段。我通过抓包确认,该图片的 Response Headers 中,确实没有返回 Content-Length 字段。由于其他原因,无法提供图片地址。

异常堆栈信息如下:

java.lang.NullPointerException: getHeaderField(...) must not be null
    at com.github.panpf.sketch.http.HurlStack$HurlResponse$contentLength$2.invoke(HurlStack.kt:150)
    at com.github.panpf.sketch.http.HurlStack$HurlResponse$contentLength$2.invoke(HurlStack.kt:149)
    at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
    at com.github.panpf.sketch.http.HurlStack$HurlResponse.getContentLength(HurlStack.kt:149)
    at com.github.panpf.sketch.fetch.HttpUriFetcher.writeCache-RgG5Fkc(HttpUriFetcher.kt:174)
    at com.github.panpf.sketch.fetch.HttpUriFetcher.access$writeCache-RgG5Fkc(HttpUriFetcher.kt:46)
    at com.github.panpf.sketch.fetch.HttpUriFetcher$executeFetch$2.invokeSuspend(HttpUriFetcher.kt:121)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
    at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:115)
    at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:103)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)

Versions

panpf commented 4 months ago

问题出在 HttpUrlStack 的 150 行,如下:

connection.getHeaderField("content-length").toLongOrNull() ?: -1
// 改为如下即可
connection.getHeaderField("content-length")?.toLongOrNull() ?: -1

下一个版本还有一段时间,所以你可以先自行拷贝 HttpUrlStack 并修改然后使用或者直接使用 okhttp 版本的 HttpStack,相关文档请参考 https://github.com/panpf/sketch/blob/main/docs/wiki/http_stack_zh.md

panpf commented 4 months ago

此 bug 已在 3.3.1 版本中修复,请更新到 3.3.1 版本