0

I have a MainActivity and class with Shared Preferences.

public class MainActivity extends AppCompatActivity {
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;

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

        if (DataStorage.getConfig(AppCore.getContext(),Const.FIRST).equals("true")){
            FragmentManager manager = getSupportFragmentManager();
            FirstDialog firstDialog = new FirstDialog();
            firstDialog.show(manager,"dialog");
            DataStorage.setConfig(AppCore.getActivity(), Const.FIRST,"false");
        }
    }
}

And a DataStorage class with Shared Preferences:

public class DataStorage {

    public static String getConfig(Context context, String name) {
        return getConfig(context, name, "");
    }

    public static String getConfig(Context context, String name, String defValue) {
        SharedPreferences settings = context.getSharedPreferences(
                Const.NAME, 0);
        String ret = settings.getString(name, defValue);
        return ret;
    }

    public static void setConfig(Context context, String name, String value) {
        SharedPreferences settings = context.getSharedPreferences(
                Const.NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(name, value);
        editor.commit();
    }
}

So I have an error that say smth like this:

E/AndroidRuntime: FATAL EXCEPTION: main
  Process: nortti.ru.dogshelter, PID: 16246
  java.lang.RuntimeException: Unable to start activity ComponentInfo{nortti.ru.dogshelter/nortti.ru.dogshelter.activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2326)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
      at android.app.ActivityThread.access$800(ActivityThread.java:147)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5264)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
      at nortti.ru.dogshelter.DataStorage.getConfig(DataStorage.java:15)
      at nortti.ru.dogshelter.DataStorage.getConfig(DataStorage.java:11)
      at nortti.ru.dogshelter.activities.MainActivity.onCreate(MainActivity.java:44)
      at android.app.Activity.performCreate(Activity.java:5975)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
      at android.app.ActivityThread.access$800(ActivityThread.java:147) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5264) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695) 

It's seamed that is womething wrong with a context in DataStorage.

Omar Aflak
  • 2,918
  • 21
  • 39
Nancy Khorevich
  • 53
  • 2
  • 11

2 Answers2

0

Replace this line:

DataStorage.getConfig(AppCore.getContext(), Const.FIRST).equals("true")

With this one:

DataStorage.getConfig(this, Const.FIRST).equals("true")
Omar Aflak
  • 2,918
  • 21
  • 39
0

The problem is on this line:

if (DataStorage.getConfig(AppCore.getContext(),Const.FIRST).equals("true")){

Somehow AppCore.getContext() returns null, causing the NullPointerException.

To fix it, make sure you pass a non-null context instance here.

H.Nguyen
  • 1,621
  • 5
  • 19
  • 31