title: How to access Appication objects from Sync Adapter
body: <p>I am working on an IM application with open fire server. I'm implementing Sync Adapter for managing the contacts.</p>
  
  <p>From my sync adapter's <code>onPerformSync()</code> if I access the connection object which I kept in my Application class it returns <em>null</em>.</p>
  
  <p>What am I doing wrong? How should I do that?</p>
  
  <p>My Application class</p>
  
  <pre><code>public class MyApp extends Application {
      private Connection connection;
      private static MyApp instance;
      public static MyApp getInstance() {
      return instance;
  }
  
  public static void setInstance(MyApp instance) {
      MyApp.instance = instance;
  }
  @Override
  public void onCreate() {
      super.onCreate();
      instance = new MyApp();
  }
  public Connection getAuthenticatedConnection() throws NullPointerException {
  
      if (instance.connection != null
              &amp;&amp; instance.connection.isAuthenticated()) {
          return instance.connection;
      } else {
  
  //  Calling service which will create connection and update the object
  
          Intent intent = new Intent(IM_Service_IntentMessaging.ACTION);
          intent = new Intent(getContext(), IM_Service_IntentMessaging.class);
          Bundle b = new Bundle();
          b.putInt("key", IM_Service_IntentMessaging.KEY_CONNECTION);
          intent.putExtras(b);
          this.context.startService(intent);
  
          throw new NullPointerException();
      }
  //service calls this method to update the object
  public void setConnection(Connection connection) {
      instance.connection = connection;
  }
  }
  </code></pre>
  
  <p>And my onPerformSync method is as follows...</p>
  
  <pre><code>@Override
  public void onPerformSync(Account account, Bundle bundle, String authority,
          ContentProviderClient provider, SyncResult syncResult) {
  
      try {
  
          this.connection = MyApp.getInstance()
                  .getAuthenticatedConnection();
  
          this.roster = this.connection.getRoster();
  
      } catch (NullPointerException e) {
                      //this line executes always
          Log.e(TAG, "connection null...ending....");
  
          return;
      }
  
      this.mAccount = account;
  
      Log.d(TAG, "...onPerformSync...");
  
      getAllContacts();
  }
  </code></pre>
  
  <p>When I tried with creating break point in Application's <code>getAuthenticatedConnection()</code> method it didn't get triggered but the service <code>IM_Service_IntentMessaging</code> which i called from there to create connection is working.</p>