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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:02:27+00:00 2026-06-12T06:02:27+00:00

Working with SharedPreferences, this activity crashes upon launching. First I’ll post the activity code,

  • 0

Working with SharedPreferences, this activity crashes upon launching. First I’ll post the activity code, and then I’ll post my LogCat. Thank you so much guys, you guys are always such big help! 🙂

Activity Code;

package com.creativecoders.gymbuddy;

import com.creativecoders.gymbuddy.R;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class Benchmark extends Activity {

public static final String GB_PREFERENCES_BENCH = "Bench";
public static final String GB_PREFERENCES_FLIES = "Flies";

SharedPreferences settings = getSharedPreferences("gBValues", 
     Context.MODE_PRIVATE);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_benchmark);
    }

public void onStart() {
    super.onStart();

    findViewById(R.id.button5).setOnClickListener(new handleButton5());

}

class handleButton5 implements OnClickListener {
    public void onClick(View v) {

        EditText editText1 = (EditText)findViewById(R.id.editText1);
        String sWeight = editText1.getText().toString();
        final double dWeight = Double.parseDouble(sWeight);

        EditText editText2 = (EditText)findViewById(R.id.editText2);
        String sPush = editText2.getText().toString();
        final double dPush = Double.parseDouble(sPush);

        EditText editText3 = (EditText)findViewById(R.id.editText3);
        String sSit = editText3.getText().toString();
        final double dSit = Double.parseDouble(sSit);

        EditText editText4 = (EditText)findViewById(R.id.editText4);
        String sPull = editText4.getText().toString();
        final double dPull = Double.parseDouble(sPull);

        double dBench = (((Math.floor(dWeight*.0664))*10)-10)+dPush;
        double dFlies = (Math.floor(((Math.floor(dBench*.6)/10)*10)));

        int iBench = (int)dBench;
        int iFlies = (int)dFlies;

        String sBench = "" + iBench;
        String sFlies = "" + iFlies;

        SharedPreferences.Editor editor1 = settings.edit();
        editor1.putString(GB_PREFERENCES_BENCH, sBench);
        editor1.commit();

        SharedPreferences.Editor editor2 = settings.edit();
        editor2.putString(GB_PREFERENCES_FLIES, sFlies);
        editor2.commit();

        TextView TextView1 = (TextView)findViewById(R.id.textView1);
        TextView1.setText(String.valueOf("Bench Press "+ iBench +" lbs"));

        TextView TextView2 = (TextView)findViewById(R.id.textView2);
        TextView2.setText(String.valueOf("Bar Curls "+ iCurls +" lbs"));
        }
    }

}

Here’s my LogCat;

09-28 21:18:52.636: E/AndroidRuntime(686): FATAL EXCEPTION: main
09-28 21:18:52.636: E/AndroidRuntime(686): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.creativecoders.gymbuddy/com.creativecoders.gymbuddy.Benchmark}: java.lang.NullPointerException
09-28 21:18:52.636: E/AndroidRuntime(686):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
09-28 21:18:52.636: E/AndroidRuntime(686):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
09-28 21:18:52.636: E/AndroidRuntime(686):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
09-28 21:18:52.636: E/AndroidRuntime(686):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
09-28 21:18:52.636: E/AndroidRuntime(686):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-28 21:18:52.636: E/AndroidRuntime(686):  at android.os.Looper.loop(Looper.java:137)
09-28 21:18:52.636: E/AndroidRuntime(686):  at android.app.ActivityThread.main(ActivityThread.java:4340)
09-28 21:18:52.636: E/AndroidRuntime(686):  at java.lang.reflect.Method.invokeNative(Native Method)
09-28 21:18:52.636: E/AndroidRuntime(686):  at java.lang.reflect.Method.invoke(Method.java:511)
09-28 21:18:52.636: E/AndroidRuntime(686):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-28 21:18:52.636: E/AndroidRuntime(686):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-28 21:18:52.636: E/AndroidRuntime(686):  at dalvik.system.NativeStart.main(Native Method)
09-28 21:18:52.636: E/AndroidRuntime(686): Caused by: java.lang.NullPointerException
09-28 21:18:52.636: E/AndroidRuntime(686):  at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:153)
09-28 21:18:52.636: E/AndroidRuntime(686):  at com.creativecoders.gymbuddy.Benchmark.<init>(Benchmark.java:35)
09-28 21:18:52.636: E/AndroidRuntime(686):  at java.lang.Class.newInstanceImpl(Native Method)
09-28 21:18:52.636: E/AndroidRuntime(686):  at java.lang.Class.newInstance(Class.java:1319)
09-28 21:18:52.636: E/AndroidRuntime(686):  at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
09-28 21:18:52.636: E/AndroidRuntime(686):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
09-28 21:18:52.636: E/AndroidRuntime(686):  ... 11 more
  • 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-12T06:02:28+00:00Added an answer on June 12, 2026 at 6:02 am

    You have to access the shared preferences AFTER onCreate is called. Or else the context would be null 🙂 that’s why you’re getting a null pointer exception

    move this line :

    SharedPreferences settings = getSharedPreferences("gBValues", 
         Context.MODE_PRIVATE);
    

    in the onCreate()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using following code to post image on Facebook wall. it's working fine.
EDIT: OK It turns out this code was working (more or less) I'd left
My main class look like this: public class Soundboard extends Activity { SharedPreferences preferences;
This code stop working, the only thing I have done is change to Android
I want to remove sharedPreference details;I did like this, but not working: SharedPreferences myRoutes
I can't get this one. Had an app that was working great. Then I
This is my first post on here so go easy on me lol! Ok
I have created a class for working with SharedPreferences . In my Activity ,
Working with H2 I get this error when I try to write a row
Working with MS Access for the first time and coming across a few problems

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.