So I keep getting the same error no matter how many times I try to tweak my code here and there. I will be posting every little bit of code that has to do with the error.
I will not include uecessary code so if my code doesn't look complete it's because I cut it out to make it easier for people to diagnose the issue!
First Class
I wrote !!! error !!! after the code that was said to have been the cause of the error according to my log.
SharedPreferences sf;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
sf = getSharedPreferences("MyData", MODE_PRIVATE);
checkCookie();
}
//function for making sure there's data
public void checkCookie() {
editor = sf.edit();
if (sf.getInt("cookies", -1) == -1) {
editor.putInt("cookies", 0);
}
}
//function for retrieving amount of cookies
public int getCookies()
{
editor = sf.edit(); !!! Error here !!!
int x = sf.getInt("cookies", 0);
retu x;
}
//function for changing amount of cookies
public void changeCookies(int x)
{
editor = sf.edit();
editor.putInt("cookies", x);
}
Second Class
I put !!! error !!! at the second error given which basically is what calls the function with the error in it.
//MyActivity is the first class
MyActivity mCookie = new MyActivity();
//function for adding cookies
public void cookieCalc(){
if (Math.abs((5-(millis/1000))) <= 0.005)
mCookie.changeCookies(100 * CB); ... much more similar code
//an if statement within a function which just calls the function getcookies and cookiecalc
if (loop == 2){
cookieCalc();
myText.setText("= " + mCookie.getCookies()); !!! this calls the function which gives error !!!
}
The error is 100% because of SharedPreferences as it worked with normal variables, but I needed to save the variables and was told this is a very easy method to do so. If all of this is still not enough and I caot explain it to you I will then paste my entire code on gitHub or another viewing platform.
Error Logs
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()' on a null object reference
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at app.z0nen.slidemenu.MyActivity.changeCookies(MyActivity.java:74)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at app.z0nen.slidemenu.menu1_Fragment.cookieCalc(menu1_Fragment.java:75)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at app.z0nen.slidemenu.menu1_Fragment$1.onClick(menu1_Fragment.java:148)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at android.view.View.performClick(View.java:5242)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at android.widget.TextView.performClick(TextView.java:10571)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:21196)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6938)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at com.android.inteal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
04-27 21:11:46.258 26975-26975/app.z0nen.menu E/AndroidRuntime: at com.android.inteal.os.ZygoteInit.main(ZygoteInit.java:1199)
