Network singleton with retrofit in android - null reference exception -
i have following singleton
public class apishared {      public string api_base_url = "http://url.com/";     public static apishared instance;     public apimanager client;      private context context;     private retrofit retrofit;      private apishared (context context) {          this.context = context;          retrofit = new retrofit.builder()                 .baseurl(api_base_url)                 .addconverterfactory(gsonconverterfactory.create())                 .build();          client = retrofit.create(apimanager.class);      }      public static apishared init(context context){          if (null == instance){             instance = new apishared(context);         }         return instance;     } }   and in mainactivity inside oncreate call
call<list<category>> call = apishared.instance.client.categories();   and when start app
java.lang.nullpointerexception: attempt read field 'com.example.me.fooder.apimanager com.example.me.fooder.apishared.client' on null object reference
what doing wrong ?
you're screwing singleton pattern. instance should private static. init() named getinstance (although that's not required) , way access instance. call like: apishared.init().client.categories();
Comments
Post a Comment