samedi 26 novembre 2016

Best pattern to handle null pointer exception

I am looking for best pattern to handle NPE(Null Pointer Exception). I am using retrofit for web api calls, but some times there may chance that some fields might be null, or that might be chance of null array too. What i am looking for, is there any way to handle all in just once?.

E.g.

Model Class

public class ModelClass{
     String name;
     String email;
     List<Items> items;
}

JSON File

{
  name:null,
  email:"NPE@jvm.com",
Items:[]
}

So after deserializing json file, when i try to get name it will through NPE

I have very large collection of model class, and practically it is wrong to put each field in try{}catch() while using it. Is there a better way to handle this kind of scenario?.

Aucun commentaire:

Enregistrer un commentaire