android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference

Maxime

I don't understand this mistake :/

I try to save the user name in a Shared Preferences but I have the null object but in my code, I have somethings : for example, if the admin connects, the user name saved is "Administrateur" but the error tells me that the object is empty.

Here is my code :

public class SharedPreferencesUtils {

    public  static final String SHARED_KEY_RESULTS= "TABLEAU";

    public SharedPreferencesUtils() {
    }

    private static WeakReference<SharedPreferences> sharedPref;
    private static Context mContext;

    private static final String PREF_NAME = "MY_PREFRRENCES";

    public synchronized static void init(Context context) {
        if (mContext == null) {
            mContext = context;
            sharedPref = new WeakReference<>(context
                    .getSharedPreferences(PREF_NAME, Activity.MODE_PRIVATE));
        }
    }

    private static SharedPreferences getSharedPref() {
        if (sharedPref == null || sharedPref.get() == null) {
            sharedPref = new WeakReference<>(mContext
                    .getSharedPreferences(PREF_NAME, Activity.MODE_PRIVATE));
        }

        return sharedPref.get();
    }


    //region STRING PREF
    public static void setStringPreference(final String key, final String value) {
        SharedPreferences.Editor editor = getSharedPref().edit();
        editor.putString(key, value);
        editor.apply();
    }

    public static String getStringPreference(final String key) {
        return getSharedPref().getString(key, null);
    }

    public static String getStringPreference(final String key, String defaultValue) {
        return getSharedPref().getString(key, defaultValue);
    }
    //endregion

    public static void removePreference(String key) {
        SharedPreferences.Editor editor = getSharedPref().edit();
        editor.remove(key);
        editor.apply();
    }


    public static void clearAllPreferences() {
        SharedPreferences.Editor editor = getSharedPref().edit();
        editor.clear();
        editor.commit();
    }

}

Main Activity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    name_user = new String[2];

    TextView login_titre = (TextView) findViewById(R.id.login_label);

    username = (EditText) findViewById(R.id.edit_user);
    password = (EditText) findViewById(R.id.edit_password);

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

    Police police = new Police();
    police.setFont(Login.this, login_titre, "LemonMilklight.otf");

    checking.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(CheckLogin.ENDPOINT)
                    .build();
            CheckLogin api = retrofit.create(CheckLogin.class);

            api.Check(
                    username.getText().toString(),
                    password.getText().toString()).enqueue(

                    new Callback<ResponseBody>() {
                        @Override
                        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                            BufferedReader reader = null;

                            String output = "";
                            try {

                            reader = new BufferedReader(new InputStreamReader(response.body().byteStream()));

                                output = reader.readLine();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                            Toast.makeText(Login.this, output, Toast.LENGTH_LONG).show();

                            name_user = output.split(" ");
                            String save_name = name_user[1];

                            try {
                                SharedPreferencesUtils.setStringPreference("Utilisateur", save_name);
                            } catch (Exception e) {
                                Log.e("Erreur", e.getMessage());
                            }

Thanks for you help. :)

Lance Toth

If you never call init(...) in SharedPreferencesUtils, mContext is always null

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference

From Dev

'java.lang.String android.content.Context.getPackageName()' on a null object reference

From Dev

error:Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

From Dev

Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object

From Java

android.content.Context.getPackageName()' on a null object reference

From Dev

android.content.Context.getContentResolver()' on a null object reference

From Dev

android.content.Context.getPackageName()' on a null object reference

From Dev

'android.content.Context.getResources()' on a null object reference

From Dev

Android. Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference

From Dev

Android. Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference

From Dev

android.content.res.Resources android.content.Context.getResources()' on a null object reference

From Dev

Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference on Volley while executing Swiperefresh Android

From Dev

Attempt to invoke virtual method 'android.os.Looper android.content.Context.getMainLooper()' on a null object reference

From Dev

'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

From Dev

Understanding Android Context: (null object reference)

From Dev

Android exception Attempt to invoke virtual method 'int android.content.Context.checkPermission(java.lang.String, int, int)

From Dev

Attempt to invoke virtual method 'android.content.Context.getResources()' on a null object reference

From Dev

Attempt to invoke virtual method 'android.content.Context.getResources()' on a null object reference

From Dev

Error: Attempt to invoke virtual method 'android.content.Context.getResources()' on a null object reference

From Dev

Exception: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

From Dev

Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

From Dev

cannot resolve constructor ArrayAdapter(android.content.Context,int,java.lang.String[],java.lang.String,java.lang.String)

From Dev

Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)

From Dev

Attempt to invoke virtual method 'android.content.Context android.support.v4.app.FragmentActivity.getApplicationContext()' on a null object reference

From Dev

Attempt to invoke virtual method 'android.content.Context com.google.android.gms.common.api.GoogleApiClient.getContext()' on a null object reference

From Dev

Service (Android Notification): Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

From Dev

NULL Pointer when Activity starts without intent. 'int java.lang.String.hashCode()' on a null object reference

From Dev

NullPointerException addToRequestQueue(com.android.volley.Request, java.lang.String)' on a null object reference

From Dev

Android - asking for GPS permissions Context.getTheme()' null object reference

Related Related

  1. 1

    Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference

  2. 2

    'java.lang.String android.content.Context.getPackageName()' on a null object reference

  3. 3

    error:Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

  4. 4

    Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object

  5. 5

    android.content.Context.getPackageName()' on a null object reference

  6. 6

    android.content.Context.getContentResolver()' on a null object reference

  7. 7

    android.content.Context.getPackageName()' on a null object reference

  8. 8

    'android.content.Context.getResources()' on a null object reference

  9. 9

    Android. Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference

  10. 10

    Android. Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference

  11. 11

    android.content.res.Resources android.content.Context.getResources()' on a null object reference

  12. 12

    Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference on Volley while executing Swiperefresh Android

  13. 13

    Attempt to invoke virtual method 'android.os.Looper android.content.Context.getMainLooper()' on a null object reference

  14. 14

    'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

  15. 15

    Understanding Android Context: (null object reference)

  16. 16

    Android exception Attempt to invoke virtual method 'int android.content.Context.checkPermission(java.lang.String, int, int)

  17. 17

    Attempt to invoke virtual method 'android.content.Context.getResources()' on a null object reference

  18. 18

    Attempt to invoke virtual method 'android.content.Context.getResources()' on a null object reference

  19. 19

    Error: Attempt to invoke virtual method 'android.content.Context.getResources()' on a null object reference

  20. 20

    Exception: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

  21. 21

    Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

  22. 22

    cannot resolve constructor ArrayAdapter(android.content.Context,int,java.lang.String[],java.lang.String,java.lang.String)

  23. 23

    Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)

  24. 24

    Attempt to invoke virtual method 'android.content.Context android.support.v4.app.FragmentActivity.getApplicationContext()' on a null object reference

  25. 25

    Attempt to invoke virtual method 'android.content.Context com.google.android.gms.common.api.GoogleApiClient.getContext()' on a null object reference

  26. 26

    Service (Android Notification): Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

  27. 27

    NULL Pointer when Activity starts without intent. 'int java.lang.String.hashCode()' on a null object reference

  28. 28

    NullPointerException addToRequestQueue(com.android.volley.Request, java.lang.String)' on a null object reference

  29. 29

    Android - asking for GPS permissions Context.getTheme()' null object reference

HotTag

Archive