Exception while updating date & time on background for Android OS ver 2.2 & above
<p>I am using one class &amp; interface for calculating/updating date &amp; time.</p>

<p><strong>The interface:</strong> </p>

<pre><code>public interface CListener {

    public void updateClock(String date, String time);

}
</code></pre>

<p><strong>The class:</strong></p>

<pre><code>public class CManager {

    private CListener mListener = null;

    private static Timer mTimer = null;

    private static String mDateData = "";

    private static String mTimeData = "";

    private static  CManager _instance = null;

    private CManager() {

        if (mTimer == null) {

            mTimer = new Timer();
            mTimer.schedule(new TimerTask() {
                @Override
                public void run() {

                    updateTime();
                }

            }, 0, 1000);
        }
    }


    public static CManager getInstance() {
          if(_instance == null ) {
             _instance = new CManager ();
          }
          return _instance;
       }


    public void setClockListener(ClockListener listener) {
        this.mListener = listener;
    }


    public void updateTime() {


            Date d = new Date();

                        // Calculating the date , month ,day ,hr ,min from Date d

            mListener.updateClock(mDateData, mTimeData);

            // mDateData -&gt; Calculated combined Date
            // mTimeData -&gt; Calculated combined time


    }


    public String getDate() {
        return mDateData;
    }


    public String getTime() {
        return mTimeData;
    }
}
</code></pre>

<p><strong>I am calling the CManager class from an activity</strong></p>

<pre><code>public class SampleActivity extends Activity{


private CManager mCManager = null;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mCManager = CManager.getInstance();

 }
</code></pre>

<p>The above code is working perfectly &amp; updating date &amp; time with Android OS version 1.6 to 2.1 .  </p>

<p>But the same code is giving the following error for Android OS version 2.2. &amp; above.</p>

<pre><code>04-08 03:57:39.269: ERROR/AndroidRuntime(419): FATAL EXCEPTION: Timer-0
04-08 03:57:39.269: ERROR/AndroidRuntime(419): java.lang.NullPointerException
04-08 03:57:39.269: ERROR/AndroidRuntime(419): at com.app.CManager.updateTime(CManager.java:144)
04-08 03:57:39.269: ERROR/AndroidRuntime(419):     at com.app.CManager$1.run(CManager.java:54)
04-08 03:57:39.269: ERROR/AndroidRuntime(419):     at java.util.Timer$TimerImpl.run(Timer.java:284)
04-08 03:57:39.301: WARN/ActivityManager(70):   Force finishing activity com.app/.SampleActivity 
</code></pre>

<p>What can be the possible reason for the problem ?
Is there any alternative/work around for this issue.</p>

<p>Kindly provide your inputs/sample code.</p>

<p>Thanks in advance.</p>

<p>Warm Regards,</p>

<p>CB</p>
