google / volley

https://google.github.io/volley
Apache License 2.0
3.38k stars 754 forks source link

Fix nullability issues in Kotlin sample code #445

Closed ghost closed 1 year ago

ghost commented 2 years ago

next is my code

val url2 = "http://ngx.cn/api/v1/cloud/auth/captcha/file/${captchaId}.png" captchaImage.setDefaultImageResId(R.color.white) captchaImage.setErrorImageResId(R.color.black) val imageLoader = SingletonVolley.getInstance(requireActivity()).imageLoader captchaImage.setImageUrl(url2, imageLoader)

ERROR

java.lang.NullPointerException: cache.get(url) must not be null at com.example.ngxmobileclient.SingletonVolley$imageLoader$2$1.getBitmap(SingletonVolley.kt:28) at com.android.volley.toolbox.ImageLoader.get(ImageLoader.java:227) at com.android.volley.toolbox.NetworkImageView.loadImageIfNecessary(NetworkImageView.java:245) at com.android.volley.toolbox.NetworkImageView.setImageUrl(NetworkImageView.java:109) at com.example.ngxmobileclient.ui.login.LoginFragment.getCaptcha$lambda-8(LoginFragment.kt:156) at com.example.ngxmobileclient.ui.login.LoginFragment.$r8$lambda$n1UUm1SdW0SH2_V01mipVoJ8p98(Unknown Source:0) at com.example.ngxmobileclient.ui.login.LoginFragment$$ExternalSyntheticLambda5.onResponse(Unknown Source:6) at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:100) at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:6878) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)

jpd236 commented 2 years ago

The crash is in com.example.ngxmobile.SingletonVolley which is a class in your app, not Volley. Seems like you're assuming that an entry will be in the Cache, which is not guaranteed.

Not much more we can do to help from the Volley side; since the issue doesn't appear to be in Volley itself, I'd probably recommend a more general purpose help medium like StackOverflow or our user group at volley-users@googlegroups.com, since this is an issue tracker for issues with Volley itself. That said, if you think there is an issue with Volley, please provide the full sample (including the class which is crashing) and explain where you think the issue is.

ghost commented 2 years ago

Thank you for your help, because I wrote this paragraph according to the example code of the singleton. I'm very sorry for the trouble.

jpd236 commented 2 years ago

Ah - that's helpful context - I didn't realize this was adapted from the sample code. I assume you're referencing https://google.github.io/volley/requestqueue.html#use-a-singleton-pattern specifically.

I think the sample code should have getBitmap return Bitmap? instead of Bitmap, since the ImageCache interface indicates the method is @Nullable. That was probably an oversight when converting the Java sample code to Kotlin. Does that change fix the issue for you?

jpd236 commented 1 year ago

The above change to the sample code should hopefully resolve the issue. Let us know if not.