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 8041239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:08:51+00:00 2026-06-05T04:08:51+00:00

I have created a class for working with SharedPreferences . In my Activity ,

  • 0

I have created a class for working with SharedPreferences. In my Activity, I am trying to add an item and get the following error…

06-05 17:01:53.950: E/AndroidRuntime(3488): FATAL EXCEPTION: main
06-05 17:01:53.950: E/AndroidRuntime(3488): java.lang.NullPointerException
06-05 17:01:53.950: E/AndroidRuntime(3488):     at com.xcxcxc.helpers.Prefs.init(Prefs.java:16)
06-05 17:01:53.950: E/AndroidRuntime(3488):     at com.xcxcxc.helpers.Prefs.addStringProperty(Prefs.java:30)
06-05 17:01:53.950: E/AndroidRuntime(3488):     at com.xcxcxc.usersadapter.SplashActivity$1.callback(SplashActivity.java:78)
06-05 17:01:53.950: E/AndroidRuntime(3488):     at com.xcxcxc.usersadapter.SplashActivity$1.callback(SplashActivity.java:1)

This is my code for handling the SharedPreferences…

import android.content.Context;
import android.content.SharedPreferences;

public class Prefs {

    public static final String STORAGE_NAME = "ApplicationPrefs";

    private static SharedPreferences settings = null;
    private static SharedPreferences.Editor editor = null;
    private static Context context = null;

    public static void init(Context cntxt) {
        context = cntxt;
    }

    private static void init() {
        settings = context.getSharedPreferences(STORAGE_NAME, 0);
        editor = settings.edit();
    }

    /* String values */
    public static void addStringProperty(String name, String value) {
        if (settings == null || editor == null) {
            init();
        }
        editor.putString(name, value);
        editor.commit();
    }

    public static String getStringProperty(String name) {
        if (settings == null || editor == null) {
            init();
        }
        return settings.getString(name, null);
    }
}

This is the code I am calling in my Activity…

Prefs.addStringProperty("client_id", "1JDkv9sdfj8sf63rjs");
  • 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-06-05T04:08:53+00:00Added an answer on June 5, 2026 at 4:08 am

    Looks like the Context is null. Set the Context before addStringProperty(..)

    Prefs.init(getApplicationContext());
    Prefs.addStringProperty("client_id", "1JDkv9sdfj8sf63rjs");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Java project where I have created a class that looks
I'm working with a complicated xml schema, for which I have created a class
Working on my assignment for Java and I have created a class called Triangle.
I have created a class for adding numbers: public class Add { private int
Does anyone have a working class or function to create the hashed email that
I have created class by implementing runnable interface and then created many threads(nearly 10)
I have a very stupid question! I have created class diagram and right now
I have created a class library with target framework 4.0 and some classes in
I have created a class to play the sound when I click the buttons.
I have created a class to play the sound when I click the buttons.

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.