Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
for some common process for activities I created a class CurrentDate, that get the actual date, for return de date in different local preferences order european (dd/mm/yy), american (mm/dd/yy... I create a some preferences data. int, Strings... the problem is load preferences data in my class, there isn't any onCreate.... because is not an activity, for this reason it doesn't have instance of load shared preferences. and give a null error:
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 com.example.ricmac.task2.CurrentDate.<init>(CurrentDate.java:50)
This class, CurrentDate is called from an activity:`
public class CurrentDate {
private static final int PREFS_MODE = 0;
private static final String PREFS_FILE = "pref";
public CurrentDate (Context context){ mContext = context;}
< here the fail in line number 50:
final SharedPreferences settings = mContext.getSharedPreferences(PREFS_FILE, PREFS_MODE);
< continue with code of CurrentDate class
public void checkDate(TextView tstart){
String iCurrent = mContext.getResources().getString(R.string.actual); // "current"
String START = tstart.getText().toString(); // get string start date
int localDatePreferencePosition = settings.getInt(KEY_LOCAL_DATE, 0); // get from shared preferences the preferences 0 European, 1 American,....
if(START.equalsIgnoreCase(iCurrent)){ // check if it the current default date
tstart.setText(getSetLocalDate(getCurrent(), localDatePreferencePosition,mContext)); // set the date in local format in textView tstar
complete Error from Logical:
2020-10-18 12:18:52.383 25809-25809/com.example.ricmac.task2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ricmac.task2, PID: 25809
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ricmac.task2/com.example.ricmac.task2.introTaskFullScreen}: 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:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
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 com.example.ricmac.task2.CurrentDate.<init>(CurrentDate.java:50)
at com.example.ricmac.task2.introTaskFullScreen.onCreate(introTaskFullScreen.java:184)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.