Skip to content
This repository has been archived by the owner on Mar 16, 2021. It is now read-only.

changeVersion doesn't support multithreading. -> NullPointerException #96

Open
toy-lin opened this issue Apr 21, 2017 · 4 comments
Open
Labels

Comments

@toy-lin
Copy link

toy-lin commented Apr 21, 2017

Very thanks for your product,but i have got a NullPointerException while migrating data,can anybody give me some advice?

java.lang.NullPointerException:
Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at net.grandcentrix.tray.core.SharedPreferencesImport.onPostMigrate(SharedPreferencesImport.java:79)
at net.grandcentrix.tray.core.SharedPreferencesImport.onPostMigrate(SharedPreferencesImport.java:38)
at net.grandcentrix.tray.core.Preferences.migrate(Preferences.java:125)
at ...module.sp.CookiePreferences.importSP(CookiePreferences.java:47)
at ..
.module.sp.CookiePreferences.onCreate(CookiePreferences.java:29)
at net.grandcentrix.tray.core.Preferences.changeVersion(Preferences.java:262)
at net.grandcentrix.tray.core.Preferences.isVersionChangeChecked(Preferences.java:292)
at net.grandcentrix.tray.core.Preferences.(Preferences.java:58)
at net.grandcentrix.tray.core.AbstractTrayPreference.(AbstractTrayPreference.java:31)
at net.grandcentrix.tray.TrayPreferences.(TrayPreferences.java:43)
at net.grandcentrix.tray.TrayPreferences.(TrayPreferences.java:48)
at ..*.module.sp.CookiePreferences.(CookiePreferences.java:23)

And below is code in CookiePreferences:

`public class CookiePreferences extends TrayPreferences {
private static final String TAG = CookiePreferences.class.getSimpleName();
public static final String MODULE = "cookie";
private static final String SP_NAME = "mycookie";
public CookiePreferences(Context context) {
super(context, MODULE, 1);
}

@Override
protected void onCreate(int initialVersion) {
    super.onCreate(initialVersion);
    importSP();
}

private void importSP() {
    final SharedPreferences sps = getContext().getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
    HashMap<String, Object> all = new HashMap<>(sps.getAll());

    Set<Map.Entry<String, Object>> set = all.entrySet();
    for (Map.Entry<String, Object> en : set) {
        String key = en.getKey();
        SharedPreferencesImport ip = new SharedPreferencesImport(getContext(), SP_NAME, key, key);
        migrate(ip);
    }
}

}`

Version:

compile 'net.grandcentrix.tray:tray:0.11.1'

@toy-lin toy-lin changed the title null pointer while migrating NullPointerException while migrating Apr 24, 2017
@toy-lin
Copy link
Author

toy-lin commented Apr 24, 2017

Oh, i have found the problem.I thought Tray is thread safe but it's not.
In my case ,method 'onCreate' was invoked twice from different thread.
Am i wrong with my code?

@toy-lin
Copy link
Author

toy-lin commented Apr 25, 2017

Yes,Tray isn't thread safe.The lock to check version change is a TrayPreferences object, so if we create more than one TrayPreferences object in different thread , method 'onCreate' would probably be invoked more than 1 time.
"synchronized void changeVersion(final int newVersion)"

@toy-lin toy-lin closed this as completed Apr 25, 2017
@passsy
Copy link
Contributor

passsy commented Apr 25, 2017

Good finding. We will fix that

@passsy passsy reopened this Apr 25, 2017
@passsy passsy added the bug label Apr 25, 2017
@passsy passsy changed the title NullPointerException while migrating changeVersion doesn't support multithreading. -> NullPointerException Apr 25, 2017
@bdelville
Copy link

The same problem would happen in onCreate if multiple process start at the same time

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

3 participants