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

  • SEARCH
  • Home
  • 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 5985939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:36:07+00:00 2026-05-22T22:36:07+00:00

in the first activity of my app, at start i am checking whether the

  • 0

in the first activity of my app, at start i am checking whether the SharedPreferrence contains some value or not. If it is to be null, it open the first activity, if not i want to open the second activity of my app.

Following is part of my code.

SharedPreferences prefs = this.getSharedPreferences( "idValue", MODE_WORLD_READABLE );
public void onCreate(Bundle savedInstanceState)
{       
   super.onCreate(savedInstanceState);  
   setContentView(R.layout.login);
  if(prefs.getString("idValue", "")==null)
    {
        userinfo();
    }
    else
    {
        Intent myIntent = new Intent(getBaseContext(), Add.class);
    startActivityForResult(myIntent, 0);
    }
}

When i checked in logcat it shows error at the following line

But my app gets crashed when the first activity gets opened

 SharedPreferences prefs = this.getSharedPreferences( "idValue", MODE_WORLD_READABLE );

Following is the my logcat details….

AndroidRuntime(5747): Uncaught handler: thread main exiting due to uncaught exception
AndroidRuntime(5747): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gs.cc.sp/com.gs.cc.sp.UserInfo}: java.lang.NullPointerException
AndroidRuntime(5747): Caused by: java.lang.NullPointerException
AndroidRuntime(5747):     at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
AndroidRuntime(5747):     at com.gs.cc.sp.UserInfo.<init>(UserInfo.java:62)
AndroidRuntime(5747):     at java.lang.Class.newInstanceImpl(Native Method)
AndroidRuntime(5747):     at java.lang.Class.newInstance(Class.java:1479)
AndroidRuntime(5747):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
AndroidRuntime(5747):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)

Please friends tell me where i am going wrong

  • 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-22T22:36:08+00:00Added an answer on May 22, 2026 at 10:36 pm

    You are accessing the current instance this of your class before initiating ,Thats why you are getting nullpointer exception.

    SharedPreferences prefs = null;
    public void onCreate(Bundle savedInstanceState)
    {       
       super.onCreate(savedInstanceState);  
       setContentView(R.layout.login);
    prefs = this.getSharedPreferences( "idValue", MODE_WORLD_READABLE );
      if(prefs.getString("idValue", "")==null)
        {
            userinfo();
        }
        else
        {
            Intent myIntent = new Intent(getBaseContext(), Add.class);
        startActivityForResult(myIntent, 0);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to start an activity when the service is ran for the first
This is my first app and i need to start new activity when the
In my App, first it shows a splash screen. After that another activity, then
I have two activities. In first I come to the second activity from first
The first activity in my app is a splash screen with a Progress Dialog
I'm trying to start a new activity in my android app, and I'm trying
This is my onCreate method of my first activity of my app. It initalizes
I have three activities where the first activity is the welcome app page(SavingsGuiderSplashActivity) which
i want to pass data between two activities.In the first activity i m using
I have three activities in my app: .OpeningScene: An opening activity that contains my

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.