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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:17:59+00:00 2026-06-02T15:17:59+00:00

I am trying to set some sharedpreferences for a user who has: name age

  • 0

I am trying to set some sharedpreferences for a user who has:

  • name
  • age
  • gender
  • weight
  • height
  • level of activity

I have implemented this code but it gives me a NullPointerException, what is the error here?

  package com.nutriapp;

  import android.app.Activity;
  import android.content.SharedPreferences;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.RadioButton;
  import android.widget.RadioGroup;

public class ProfilePreferenceActivity extends Activity {

RadioButton rb_male;
RadioButton rb_female;
RadioButton rb_light;
RadioButton rb_moderate;
RadioButton rb_heavy;
EditText name;
EditText age, weight, height;
RadioGroup genderradiogroup;
RadioGroup levelofactivity;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.createprofile);
    SharedPreferences customSharedPreference = getSharedPreferences(
            "myCustomSharedPrefs", Activity.MODE_PRIVATE);

    name = (EditText) findViewById(R.id.namefield);
    name.setText(customSharedPreference.getString("namepref", ""));

    age = (EditText) findViewById(R.id.agefield);
//  int Age = Integer.parseInt(age.getText().toString());
    age.setText(customSharedPreference.getString("agepref", ""));

    genderradiogroup = (RadioGroup) findViewById(R.id.radioGroup2);
    rb_male = (RadioButton) findViewById(R.id.maleradiobutton);
    rb_female = (RadioButton) findViewById(R.id.femaleradiobutton);
    genderradiogroup.check(customSharedPreference.getInt("genderprefs",
            rb_male.getId()));

    weight = (EditText) findViewById(R.id.weightfield);
    //int Weight = Integer.parseInt(weight.getText().toString());
    weight.setText(customSharedPreference.getString("weightpref", ""));

    height = (EditText) findViewById(R.id.heightfield);
    //int Height = Integer.parseInt(height.getText().toString());
    weight.setText(customSharedPreference.getString("heightpref", ""));

    RadioGroup levelofactivity = (RadioGroup) findViewById(R.id.radioGroup2);
    rb_light = (RadioButton) findViewById(R.id.lightradiobutton);
    rb_moderate = (RadioButton) findViewById(R.id.moderateradiobutton);
    rb_heavy=(RadioButton) findViewById(R.id.heavyradiobutton);
    levelofactivity.check(customSharedPreference.getInt("levelpref", rb_light.getId()));


    Button addUser = (Button) findViewById(R.id.checkcreateprofilebutton);



    addUser.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            savePreferences();
            finish();

        }
    });

}

private void savePreferences(){

    SharedPreferences customSharedPreference = getSharedPreferences("myCustomSharedPrefs", Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = customSharedPreference.edit();
    editor.putString("namepref", name.getText().toString());
    editor.putString("agepref", age.getText().toString());
    editor.putInt("genderprefs", genderradiogroup.getCheckedRadioButtonId());
    editor.putString("heightpref", height.getText().toString());
    editor.putString("weightpref", weight.getText().toString());
    editor.putInt("levelpref", levelofactivity.getCheckedRadioButtonId());
    editor.commit(); 
  }   
}

here is the log :

04-24 13:36:00.062: E/AndroidRuntime(797): FATAL EXCEPTION: main
04-24 13:36:00.062: E/AndroidRuntime(797): java.lang.NullPointerException
04-24 13:36:00.062: E/AndroidRuntime(797):  at com.nutriapp.ProfilePreferenceActivity.savePreferences(ProfilePreferenceActivity.java:86)
04-24 13:36:00.062: E/AndroidRuntime(797):  at com.nutriapp.ProfilePreferenceActivity.access$0(ProfilePreferenceActivity.java:77)
04-24 13:36:00.062: E/AndroidRuntime(797):  at com.nutriapp.ProfilePreferenceActivity$1.onClick(ProfilePreferenceActivity.java:69)
04-24 13:36:00.062: E/AndroidRuntime(797):  at android.view.View.performClick(View.java:3511)
04-24 13:36:00.062: E/AndroidRuntime(797):  at android.view.View$PerformClick.run(View.java:14105)
04-24 13:36:00.062: E/AndroidRuntime(797):  at android.os.Handler.handleCallback(Handler.java:605)
04-24 13:36:00.062: E/AndroidRuntime(797):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-24 13:36:00.062: E/AndroidRuntime(797):  at android.os.Looper.loop(Looper.java:137)
04-24 13:36:00.062: E/AndroidRuntime(797):  at android.app.ActivityThread.main(ActivityThread.java:4424)
04-24 13:36:00.062: E/AndroidRuntime(797):  at java.lang.reflect.Method.invokeNative(Native Method)
04-24 13:36:00.062: E/AndroidRuntime(797):  at java.lang.reflect.Method.invoke(Method.java:511)
04-24 13:36:00.062: E/AndroidRuntime(797):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-24 13:36:00.062: E/AndroidRuntime(797):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-24 13:36:00.062: E/AndroidRuntime(797):  at dalvik.system.NativeStart.main(Native Method)
  • 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-02T15:18:04+00:00Added an answer on June 2, 2026 at 3:18 pm

    You are not doing setText to height in onCreate.. Be careful with copy pasting..

    You are redeclaring the levelofactivity variable inside the onCreate method. It should be a member..

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

Sidebar

Related Questions

I have a Perl script that's trying to set some configured DateTime and DateTime::Duration
I have been trying to set up some symbolic links in the Terminal, and
I'm trying to set some booleans based on whether or not an object has
I'm trying to set up some tests for a website that has a forum.
I'm trying to set some javascript data (json) in my markup by calling a
I am trying to set some text on a label descriptionLabel.text = [NSString stringWithFormat:mySTUser.bio];
I am trying to set some default inherited permissions to a directory that will
I'm trying to set some custom AWS CloudWatch metrics using the Java SDK. I
I'm iterating through some objects and trying to set some properties that, apparently, don't
In my ruby(1.9.3) on rails (3.1) app I'm trying to set some conditional behavior

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.