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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:53:44+00:00 2026-05-20T15:53:44+00:00

So, i just want to save to a file when taping on a button,

  • 0

So, i just want to save to a file when taping on a button, however, i have a FC, here is the code:

final Button button = (Button) findViewById(R.id.button);

 button.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {
 String value1 = vol.getText().toString();
 String value2 = kil.getText().toString();


                     if (value1 != null && value1.trim().length() > 0 && value2 != null && value2.trim().length() > 0)
                     {
                             float q1=Float.parseFloat(vol.getText().toString());
                             float q2=Float.parseFloat(kil.getText().toString());
                             float x=((q1 / q2)* 100);
                             String y= Float.toString(x);
                             cons.setText(y);

                             SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
                             data = format.format(new Date());
                             data = data + " : " + y + " L/100km\n" + value1 + " litres "+ value2 + " km\n";
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
      if (data != "" ) {
      String fileName = getResources().getString(R.string.fileName);
      String fileDir = ""+ preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
      myIO.WriteSettings(context, fileDir + fileName, data);
      data = "";
                                }

                     }
                     else 
                     {
                     Toast.makeText(carburant.this, "Veuillez vérifier les deux champs", Toast.LENGTH_LONG).show();
                     }
     }

});

Logcat:
03-16 20:52:59.080: ERROR/AndroidRuntime(1019): FATAL EXCEPTION: main
03-16 20:52:59.080: ERROR/AndroidRuntime(1019): java.lang.NullPointerException 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:353) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:348) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at carburant.android.com.carburant$1.onClick(carburant.java:118) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at android.view.View.performClick(View.java:2485) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at android.view.View$PerformClick.run(View.java:9080) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at android.os.Handler.handleCallback(Handler.java:587) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at android.os.Handler.dispatchMessage(Handler.java:92) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at android.os.Looper.loop(Looper.java:123) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at android.app.ActivityThread.main(ActivityThread.java:3683) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at java.lang.reflect.Method.invokeNative(Native Method) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at java.lang.reflect.Method.invoke(Method.java:507) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at dalvik.system.NativeStart.main(Native Method) 03-16 20:52:59.080: WARN/ActivityManager(68): Force finishing activity carburant.android.com/.carburant 03-16 20:52:59.854: WARN/ActivityManager(68): Activity pause timeout for HistoryRecord{406e89b8 carburant.android.com/.carburant}

Any help please ?
Thanks 🙂 .

  • 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-20T15:53:44+00:00Added an answer on May 20, 2026 at 3:53 pm

    According to your stack trace and extra information, the error is a null pointer at the line

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); 
    

    I suspect that the context parameter is null. Is context a variable name which you’ve declared and assigned to a context already?

    EDIT:

    you need to assign a Context to your context variable. Just declaring it as a global variable without assigning it makes it null.

    Please try this:

    private Context context = getApplicationContext();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just want to enable or disable the button inside a ControlTemplate for a
Just want to know if there other optional wrapper framework available that generate client
I just want an ASP.NET DropDownList with no selected item. Setting SelectedIndex to -1
I just want a quick way (and preferably not using a while loop)of createing
I just want my apache to register some of my predefined environment so that
I just want a simple tool that will help me quickly write scripts/packages that
I just want to see what files were modded/added/deleted between 2 arbitrary revisions. How
I just want to clarify one thing. This is not a question on which
I just want to add a couple subdomains to my computers FQDN so that
I just want to send SMS from my web application in PHP. Can anyone

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.