Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.8.5;NullPointerException;TypeAdapterFactory #1578

Closed
geolo123 opened this issue Sep 18, 2019 · 13 comments
Closed

2.8.5;NullPointerException;TypeAdapterFactory #1578

geolo123 opened this issue Sep 18, 2019 · 13 comments

Comments

@geolo123
Copy link

java.lang.NullPointerException: Attempt to invoke interface method 'com.google.gson.TypeAdapter com.google.gson.TypeAdapterFactory.create(com.google.gson.Gson, com.google.gson.reflect.TypeToken)' on a null object reference
at com.google.gson.Gson.getAdapter(Gson.java:458)
at com.google.gson.Gson.fromJson(Gson.java:926)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.google.gson.Gson.fromJson(Gson.java:813)

@geolo123
Copy link
Author

help

@Omega-Ariston
Copy link
Contributor

Omega-Ariston commented Sep 29, 2019

It would be good if you can provide the context that someone else can reproduce this error from :)

@aalaprawat
Copy link

any updates on this ?

@eamonnmcmanus
Copy link
Member

@aalaprawat if you are experiencing a similar exception, perhaps you could add some details about what you are doing when the exception occurs? What the Gson setup is and what the JSON input is that triggers the problem.

@aalaprawat
Copy link

aalaprawat commented Dec 8, 2021

Iam not able to replicate this issue on my device. But various devices are facing this issue across various android api levels in production app.

Fatal Exception: java.lang.NullPointerException: Attempt to invoke interface method 'com.google.gson.TypeAdapter com.google.gson.TypeAdapterFactory.create(com.google.gson.Gson, com.google.gson.reflect.TypeToken)' on a null object reference at com.google.gson.Gson.getAdapter(Gson.java:458) at com.google.gson.Gson.toJson(Gson.java:696) at com.google.gson.Gson.toJson(Gson.java:683) at com.google.gson.Gson.toJson(Gson.java:638) at com.google.gson.Gson.toJson(Gson.java:618) at in.workindia.nileshdungarwal.helpers.JsonHelper.convertJobContentsToJson(JsonHelper.java:322) at in.workindia.nileshdungarwal.data_parser.ContentValuesParser.parseJobs(ContentValuesParser.java:335) at in.workindia.nileshdungarwal.data_parser.ContentValuesParser.parseNewJobs(ContentValuesParser.java:70) at in.workindia.nileshdungarwal.retrofit.RetrofitSyncAll.finalProcessOfJobs(RetrofitSyncAll.java:844) at in.workindia.nileshdungarwal.retrofit.RetrofitSyncAll$101.onResponse(RetrofitSyncAll.java:6591) at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.lambda$onResponse$0(DefaultCallAdapterFactory.java:81) at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.lambda$onResponse$0$DefaultCallAdapterFactory$ExecutorCallbackCall$1(DefaultCallAdapterFactory.java:3) at retrofit2.-$$Lambda$DefaultCallAdapterFactory$ExecutorCallbackCall$1$hVGjmafRi6VitDIrPNdoFizVAdk.run(-.java:3) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:246) at android.app.ActivityThread.main(ActivityThread.java:8595) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

@Marcono1234
Copy link
Collaborator

Unfortunately Gson does not check for null for the GsonBuilder.registerTypeAdapterFactory method. Could you please check whether by accident you are calling it with null as value?

@aalaprawat
Copy link

@Marcono1234

gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(JsonNode.class, new JsonNodeSerializer()); gsonBuilder.registerTypeAdapter(JsonNode.class, new JsonNodeDeserializer()); Gson gson = gsonBuilder.create();

I don't think this could be null

@Marcono1234
Copy link
Collaborator

@aalaprawat, were you able to reproduce this locally with a debug build of your application as well? Could you please try to create a small self-contained sample project using the latest Gson version which shows this issue?

@aalaprawat
Copy link

Currently, I am using Gson 2.8.6, would upgrading resolve the issue?
@Marcono1234

@Marcono1234
Copy link
Collaborator

I am afraid using the latest version most likely does not make a big difference, but in case this is really a Gson bug it would avoid having to track down bugs in an old Gson version. Future Gson versions will also add validation for the GsonBuilder methods (#2179), maybe that would help track down the bug.

The stack trace suggests that somehow null was added as element to the Gson.factories list; but I don't see how that could have happened, other than by a call to GsonBuilder.registerTypeAdapterFactory(null).

Since you mentioned it only occurs on some devices, maybe there is something wrong with those devices, or the Android version they are running.

@aalaprawat
Copy link

facing issues across all the devices and API versions, As mentioned above through code snippet, I can't point where GsonBuilder.registerTypeAdapterFactory(null) could happen

@Marcono1234
Copy link
Collaborator

Marcono1234 commented Feb 3, 2024

Are you still experiencing this with the latest Gson version (currently 2.10.1)? It includes the null checks for GsonBuilder.registerTypeAdapterFactor; maybe that allows tracking down the issue.

@Marcono1234
Copy link
Collaborator

I am going to close this because no answer has been provided. If you still experience this with the latest Gson version, feel free to comment here again with more details.

@Marcono1234 Marcono1234 closed this as not planned Won't fix, can't repro, duplicate, stale May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants