title: Unable to start activity ComponentInfo java.lang.NullPointerException
body: <p>Actually I have two java classes of which one is an activity class. Now from that activity class, I want to call a function of second class which is not in an activity class. So all works fine but when I use SharedPreferences inside that function it shows me an error <code>Unable to start activity ComponentInfo java.lang.NullPointerException</code>. Please, anyone help.</p>
  <p><strong>code for first java file:</strong></p>
  <pre><code>public class SplashScreen extends Activity 
  {
      public void onCreate(Bundle savedInstanceState) 
      {
            super.onCreate(savedInstanceState);
            
            setContentView(R.layout.splash_screen);
            
            
            CycleManager.getSingletonObject().test();//call functions of another class
            
      }
  }
  </code></pre>
  <p><strong>code for second java file:</strong></p>
  <pre><code>public class CycleManager 
  {       
      
      private static CycleManager cycleMangrObject;       
      
      private CycleManager() {            
              onInitialization(); 
              
              //Compute averages using data loaded from register
              ComputeAverages();
      }
      
      public static synchronized CycleManager getSingletonObject() {          
              if (cycleMangrObject == null) {
              cycleMangrObject = new CycleManager();
              }
              
              return cycleMangrObject;
      }
      
      public Object clone() throws CloneNotSupportedException {           
              throw new CloneNotSupportedException();
          
      }
      
      public void test()
      {
          SharedPreferences preferences1 =getSharedPreferences(&quot;myPreferences&quot;, 0);
      }
  
      
  public void setAlertOnDevice(){                 
                  //Delete data
                  Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + &quot;events&quot;);
                  int id = 1; // calendar entry ID
                  ContentResolver cr = getContentResolver();
                  EVENTS_URI= ContentUris.withAppendedId(EVENTS_URI, id);
                  cr.delete(EVENTS_URI, &quot;calendar_id=1&quot;, null);
                  Resources res=getResources();
                  
                  //set Alerts in device calendar
                  Date dtStartDate = CycleManager.getSingletonObject().getStartDate();
                      
                  boolean bDeleteAndReturn = false;
                  
                  Calendar cal = Calendar.getInstance();
                  
                  
                  if (dtStartDate.getTime() ==  CycleManager.getSingletonObject().getDefaultDate().getTime())
                  {
                          bDeleteAndReturn = true;
                          dtStartDate = cal.getTime();
                  }  
                                  
                  getOffsetsForCycleStages(CycleManager.getSingletonObject().iAvgCycleTime);
                  
                  if(bDeleteAndReturn==false)
                  {
                      if (CycleManager.getSingletonObject().bNextCycleAlert &amp;&amp; iStart&gt;0)
                      {
                          cal.setTime(dtStartDate);
                          cal.add(Calendar.DATE, iStart);                   
                         // ContentResolver cr = getContentResolver();
                          String str=res.getString(R.string.alert_start);
                          String strDescription=res.getString(R.string.alert_start_msg);
                          ContentValues values = new ContentValues();
                          values.put(&quot;calendar_id&quot;, 1);
                          values.put(&quot;title&quot;, str);
                          values.put(&quot;description&quot;, strDescription);
                          values.put(&quot;dtstart&quot;, cal.getTimeInMillis()); 
                          values.put(&quot;dtend&quot;, cal.getTimeInMillis()); 
                          cr.insert(EVENTS_URI, values);
                      }
                      if (CycleManager.getSingletonObject().bSafeAlert)
                      {
                           if (iSafe1&gt;0)
                           {
                              cal.setTime(dtStartDate);
                              cal.add(Calendar.DATE, iSafe1);                       
                            //  ContentResolver cr = getContentResolver();
                              String str=res.getString(R.string.alert_safe);
                              String strDescription=res.getString(R.string.alert_safe_msg) + &quot; &quot; + new Integer(iUnsafe1-iSafe1-1);
                              ContentValues values = new ContentValues();
                              values.put(&quot;calendar_id&quot;, 1);
                              values.put(&quot;title&quot;, str);
                              values.put(&quot;description&quot;, strDescription);
                              values.put(&quot;dtstart&quot;, cal.getTimeInMillis()); 
                              values.put(&quot;dtend&quot;, cal.getTimeInMillis()); 
                              cr.insert(EVENTS_URI, values);
                           }
                           if (iSafe2&gt;0)
                           {
                               cal.setTime(dtStartDate);
                               cal.add(Calendar.DATE, iSafe2);                           
                             //  ContentResolver cr = getContentResolver();
                               String str=res.getString(R.string.alert_safe);
                               String strDescription=res.getString(R.string.alert_safe_msg) + &quot; &quot; + new Integer(CycleManager.getSingletonObject().iAvgCycleTime-iSafe2-1);
                               ContentValues values = new ContentValues();
                               values.put(&quot;calendar_id&quot;, 1);
                               values.put(&quot;title&quot;, str);
                               values.put(&quot;description&quot;, strDescription);
                               values.put(&quot;dtstart&quot;, cal.getTimeInMillis()); 
                               values.put(&quot;dtend&quot;, cal.getTimeInMillis()); 
                               cr.insert(EVENTS_URI, values);     
                           }
                      }
                      if (CycleManager.getSingletonObject().bUnsafeAlert)
                      {
                           if (iUnsafe1&gt;0)
                           {
                               cal.setTime(dtStartDate);
                               cal.add(Calendar.DATE, iUnsafe1);                         
                              // ContentResolver cr = getContentResolver();
                               String str=res.getString(R.string.alert_unsafe);
                               String strDescription=res.getString(R.string.alert_unsafe_msg) + &quot; &quot; + new Integer(iFertile-iUnsafe1-1);
                               ContentValues values = new ContentValues();
                               values.put(&quot;calendar_id&quot;, 1);
                               values.put(&quot;title&quot;, str);
                               values.put(&quot;description&quot;, strDescription);
                               values.put(&quot;dtstart&quot;, cal.getTimeInMillis()); 
                               values.put(&quot;dtend&quot;, cal.getTimeInMillis()); 
                               cr.insert(EVENTS_URI, values);
                           }
                           if (iUnsafe2&gt;0)
                           {
                               cal.setTime(dtStartDate);
                               cal.add(Calendar.DATE, iUnsafe2);
                              // ContentResolver cr = getContentResolver();
                               String str=res.getString(R.string.alert_unsafe);
                               String strDescription=res.getString(R.string.alert_unsafe_msg) + &quot; &quot; + new Integer(iSafe2-iUnsafe2-1);
                               ContentValues values = new ContentValues();
                               values.put(&quot;calendar_id&quot;, 1);
                               values.put(&quot;title&quot;, str);
                               values.put(&quot;description&quot;, strDescription);
                               values.put(&quot;dtstart&quot;, cal.getTimeInMillis()); 
                               values.put(&quot;dtend&quot;, cal.getTimeInMillis()); 
                               cr.insert(EVENTS_URI, values);
                           }
                      }
                      if (CycleManager.getSingletonObject().bFertileAlert &amp;&amp; iFertile&gt;0)
                      {
                          cal.setTime(dtStartDate);
                          cal.add(Calendar.DATE, iFertile);
                         // ContentResolver cr = getContentResolver();
                          String str=res.getString(R.string.alert_fertile);
                          String strDescription=res.getString(R.string.alert_fertile_msg) + &quot; &quot; + new Integer(iUnsafe2-iFertile-1);
                          ContentValues values = new ContentValues();
                          values.put(&quot;calendar_id&quot;, 1);
                          values.put(&quot;title&quot;, str);
                          values.put(&quot;description&quot;, strDescription);
                          values.put(&quot;dtstart&quot;, cal.getTimeInMillis()); 
                          values.put(&quot;dtend&quot;, cal.getTimeInMillis()); 
                          cr.insert(EVENTS_URI, values);
                      }
                      if (CycleManager.getSingletonObject().bPMSAlert)
                      {
                          cal.setTime(dtStartDate);
                          cal.add(Calendar.DATE, iStart-7);                     
                        //  ContentResolver cr = getContentResolver();
                          String str=res.getString(R.string.alert_pms);
                          String strDescription=res.getString(R.string.alert_pms_msg);
                          ContentValues values = new ContentValues();
                          values.put(&quot;calendar_id&quot;, 1);
                          values.put(&quot;title&quot;, str);
                          values.put(&quot;description&quot;, strDescription);
                          values.put(&quot;dtstart&quot;, cal.getTimeInMillis()); 
                          values.put(&quot;dtend&quot;, cal.getTimeInMillis()); 
                          cr.insert(EVENTS_URI, values);
                      }
                  }
      }
      
      private String getCalendarUriBase(Activity act){            
                  String calendarUriBase = null;
                  Uri calendars = Uri.parse(&quot;content://calendar/calendars&quot;);
                  Cursor managedCursor = null;
                  
                  try 
                  {
                      managedCursor = act.managedQuery(calendars, null, null, null, null);
                  } 
                  catch (Exception e) {}
                  
                  if (managedCursor != null) 
                  {
                      calendarUriBase = &quot;content://calendar/&quot;;
                  }              
                  else 
                  {
                      calendars = Uri.parse(&quot;content://com.android.calendar/calendars&quot;);
                      try 
                      {
                          managedCursor = act.managedQuery(calendars, null, null, null, null);
                      } 
                      catch (Exception e){}
                      
                      if (managedCursor != null) 
                      {
                          calendarUriBase = &quot;content://com.android.calendar/&quot;;
                      }
                  }
                  return calendarUriBase;
      }
  </code></pre>