I'm getting a response from Server and I deserialize it using Gson to the corresponding Object.
The problem is that sometimes I get an error response ( which is ofcourse not of type of the Object I expect ) and that leads to NullPointerException. How can I handle such a case?
My code is :
Response pollsResponse = okHttpClient.newCall(pollsRequest).execute();
String pollsResponseString = pollsResponse.body().string();
// Gson Type for List of Poll.class oObjects
Type listType = new TypeToken<ArrayList<Poll>>() { }.getType();
pollsList = gson.fromJson(pollsResponseString, listType);
When there's an error last line of code gives NPE. How am I going to check if item is not type of ArrayList<Poll>
so I won't deserialize it ?
Edit: I can't understand why my question is similar to the one linked ? I know what is a NPE. I don't know how I can handle a result I don't expect from a network call which will lead to NPE. Correct my if I'm wrong.