Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6205479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:16:55+00:00 2026-05-24T05:16:55+00:00

Actually I have two java classes of which one is an activity class. Now

  • 0

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 Unable to start activity ComponentInfo java.lang.NullPointerException. Please, anyone help.

code for first java file:

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 for second java file:

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("myPreferences", 0);
    }

    
public void setAlertOnDevice(){                 
                //Delete data
                Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events");
                int id = 1; // calendar entry ID
                ContentResolver cr = getContentResolver();
                EVENTS_URI= ContentUris.withAppendedId(EVENTS_URI, id);
                cr.delete(EVENTS_URI, "calendar_id=1", 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 && iStart>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("calendar_id", 1);
                        values.put("title", str);
                        values.put("description", strDescription);
                        values.put("dtstart", cal.getTimeInMillis()); 
                        values.put("dtend", cal.getTimeInMillis()); 
                        cr.insert(EVENTS_URI, values);
                    }
                    if (CycleManager.getSingletonObject().bSafeAlert)
                    {
                         if (iSafe1>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) + " " + new Integer(iUnsafe1-iSafe1-1);
                            ContentValues values = new ContentValues();
                            values.put("calendar_id", 1);
                            values.put("title", str);
                            values.put("description", strDescription);
                            values.put("dtstart", cal.getTimeInMillis()); 
                            values.put("dtend", cal.getTimeInMillis()); 
                            cr.insert(EVENTS_URI, values);
                         }
                         if (iSafe2>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) + " " + new Integer(CycleManager.getSingletonObject().iAvgCycleTime-iSafe2-1);
                             ContentValues values = new ContentValues();
                             values.put("calendar_id", 1);
                             values.put("title", str);
                             values.put("description", strDescription);
                             values.put("dtstart", cal.getTimeInMillis()); 
                             values.put("dtend", cal.getTimeInMillis()); 
                             cr.insert(EVENTS_URI, values);     
                         }
                    }
                    if (CycleManager.getSingletonObject().bUnsafeAlert)
                    {
                         if (iUnsafe1>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) + " " + new Integer(iFertile-iUnsafe1-1);
                             ContentValues values = new ContentValues();
                             values.put("calendar_id", 1);
                             values.put("title", str);
                             values.put("description", strDescription);
                             values.put("dtstart", cal.getTimeInMillis()); 
                             values.put("dtend", cal.getTimeInMillis()); 
                             cr.insert(EVENTS_URI, values);
                         }
                         if (iUnsafe2>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) + " " + new Integer(iSafe2-iUnsafe2-1);
                             ContentValues values = new ContentValues();
                             values.put("calendar_id", 1);
                             values.put("title", str);
                             values.put("description", strDescription);
                             values.put("dtstart", cal.getTimeInMillis()); 
                             values.put("dtend", cal.getTimeInMillis()); 
                             cr.insert(EVENTS_URI, values);
                         }
                    }
                    if (CycleManager.getSingletonObject().bFertileAlert && iFertile>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) + " " + new Integer(iUnsafe2-iFertile-1);
                        ContentValues values = new ContentValues();
                        values.put("calendar_id", 1);
                        values.put("title", str);
                        values.put("description", strDescription);
                        values.put("dtstart", cal.getTimeInMillis()); 
                        values.put("dtend", 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("calendar_id", 1);
                        values.put("title", str);
                        values.put("description", strDescription);
                        values.put("dtstart", cal.getTimeInMillis()); 
                        values.put("dtend", cal.getTimeInMillis()); 
                        cr.insert(EVENTS_URI, values);
                    }
                }
    }
    
    private String getCalendarUriBase(Activity act){            
                String calendarUriBase = null;
                Uri calendars = Uri.parse("content://calendar/calendars");
                Cursor managedCursor = null;
                
                try 
                {
                    managedCursor = act.managedQuery(calendars, null, null, null, null);
                } 
                catch (Exception e) {}
                
                if (managedCursor != null) 
                {
                    calendarUriBase = "content://calendar/";
                }              
                else 
                {
                    calendars = Uri.parse("content://com.android.calendar/calendars");
                    try 
                    {
                        managedCursor = act.managedQuery(calendars, null, null, null, null);
                    } 
                    catch (Exception e){}
                    
                    if (managedCursor != null) 
                    {
                        calendarUriBase = "content://com.android.calendar/";
                    }
                }
                return calendarUriBase;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:16:55+00:00Added an answer on May 24, 2026 at 5:16 am

    Its not possible because the other class does’t know there something called SharedPreference.. so

    public void test(Context c)
        {
            SharedPreferences preferences1 =c.getSharedPreferences("myPreferences", 0);
        }
    

    and while calling

    CycleManager.getSingletonObject().test(this);//call functions of another class
    

    and again

    c.getContentResolver().delete(EVENTS_URI, null, null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need an XML-serializable dictionary. Actually, I now have two quite different programs that
I have Process objects that are monitored from two different views. A Windows.Forms.ListView (actually
I actually have two questions regarding exception/error handling in the iPhone app that I
I actually have right now two questions: 1) What font faces are preferred for
I have a databound listbox which is actually displaying two columns of data. It
I have the following scenario (in Java / Hibernate): I have two entity classes:
In Java, I have a subclass Vertex of the Java3D class Point3f . Now
I have two servers, written in Java, that communicate through sockets and TCP. The
I have two ArrayList's that I am using, one for the enemy sprites and
I have an activity (DemoAppActivity) from which I am trying to launch a different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.