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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:45:54+00:00 2026-06-15T04:45:54+00:00

Using sharedpreferences I run this method: private Button button1; private Button button2; private TextView

  • 0

Using sharedpreferences I run this method:

private Button button1;
private Button button2;
private TextView textView1;

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

// here save preferences if there are not
// after second start of program, checkIndex(); goes to that crash

public void checkIndex(){
    SharedPreferences prefs = getSharedPreferences(PRIVATE_PREF, 0);
    String index = prefs.getString("scelta", "");
    if (index == ""){
 index();
    }
    else {

        String scelta = prefs.getString("scelta", "");
        int a = prefs.getInt("arraystart", 0);
        int b = prefs.getInt("arrayend", 0);

        int size = prefs.getInt(prog +"_size", 0);
        String prog[] = new String[size];
        for(int i=0; i<prog.length; i++)

         prog[i] = prefs.getString(prog + "_" + i, null);

        String titolo = prefs.getString("titolo", "");
        int x = prefs.getInt("x", 0);

        textView1.setText(""+a+" "+b+" "+titolo);

I’ve a NullPointer exception by textView1 and I don’t understand why! (I spent many many hours but nothing)… why? thanks!

logcat:

11-30 21:49:42.806: E/AndroidRuntime(3739): FATAL EXCEPTION: main
11-30 21:49:42.806: E/AndroidRuntime(3739): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.perledisaggezza/com.example.perledisaggezza.MainActivity}: java.lang.NullPointerException
11-30 21:49:42.806: E/AndroidRuntime(3739):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at android.os.Looper.loop(Looper.java:137)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at java.lang.reflect.Method.invokeNative(Native Method)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at java.lang.reflect.Method.invoke(Method.java:511)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at dalvik.system.NativeStart.main(Native Method)
11-30 21:49:42.806: E/AndroidRuntime(3739): Caused by: java.lang.NullPointerException
11-30 21:49:42.806: E/AndroidRuntime(3739):     at com.example.perledisaggezza.MainActivity.checkIndex(MainActivity.java:480)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at com.example.perledisaggezza.MainActivity.onCreate(MainActivity.java:149)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at android.app.Activity.performCreate(Activity.java:5008)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-30 21:49:42.806: E/AndroidRuntime(3739):     ... 11 more
11-30 21:51:08.107: E/Trace(3796): error opening trace file: No such file or directory (2)
  • 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-15T04:45:55+00:00Added an answer on June 15, 2026 at 4:45 am

    You are calling checkIndex() before doing textView1 = (TextView)findViewById(R.id.textView1); You need to first initiatlize textView1 then you can work with it, otherwise checkIndex() is working with a null reference. Therefore, you need to add textView1 = (TextView)findViewById(R.id.textView1); before checkIndex();

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

Sidebar

Related Questions

I'm using SharedPreferences to persists Object fields. Take this example: class Item { private
I am trying to save some variables using SharedPreferences . In my onCreate() method
I am using this code to create a BroadcastReceiver that has to run even
I am using SharedPreferences. The problem I have is when I press Back button
My first time using sharedPreferences and i can't seem to get past this error.
I was wondering about how saving variables work by using SharedPreferences inside onStop() function.
My app has the ability to backup and restore SharedPreferences using an BackupAgentHelper class.
I'm writing login capability using share preference. I use SharedPreferences.Editor::commit() to store username and
I created a sample application using Android TextView with shared preferences . In my
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question

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.