Reputation: 20177
For the life of me I cannot figure out why I can’t get this method to enter the if statement.
protected void foo() {
Date d = new Date();
long now = d.getTime();
long start;
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
start = settings.getLong(FIRST_USE_DATE, 0);
Log.w(this.getClass().getName(), Long.toString(start));
if (start == 0) {
SharedPreferences.Editor editor = settings.edit();
editor.putLong(FIRST_USE_DATE, now);
}
return true; }
Note that the log and debug mode shows that “start = 0”
I also tried
if (start == 0l) {
if (start == 0L) {
What the heck am I missing here? Does 0 != 0?
I’m developing in Eclipse with Java for Android. Thanks.
Edits:
@methodin - no sorry, that does not work.
@Aioobe - I have a breakpoint under the IF statement, that never gets made.
Edit2: Here is the actual code I'm running since you've asked.
protected boolean isDemoExpired() {
Date d = new Date();
long now = d.getTime();
long demoStart;
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
demoStart = settings.getLong(FIRST_USE_DATE, 0);
if (demoStart == 0) {
SharedPreferences.Editor editor = settings.edit();
editor.putLong(FIRST_USE_DATE, now);
System.out.println(Long.toString(demoStart));
return false;
}
return true;
}
Upvotes: 0
Views: 21017
Reputation: 1538
Are you familiar with the concepts of hiding and shadowing? Do you have any other variables using the same names in this class or one of its parents?
I don't think there is anything wrong with your code - try rebooting your computer (its a Microsoft OS right? :)
If that don't work delete the offending code (end up with an empty method) compile and test to ensure there are no bugs. Then hardcode an acceptable value and test again. Finally retype your code - do not paste a copy.
Once, a very long time ago, I had something similar and it did not go away until I re-entered the code - my best guess is some hidden character was causing problems, but I have never seen it again (nor am I certain what the hell happened that time either).
Upvotes: 0
Reputation: 5106
I think your problem is exactly the opposite. I've just debugged your code and it works, the problem is that the if statement it's always true because there's no editor.commit() after making the changes to the FIRST_USE_DATE variable.
protected void foo() {
Date d = new Date();
long now = d.getTime();
long start;
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
start = settings.getLong(FIRST_USE_DATE, 0);
Log.w(this.getClass().getName(), Long.toString(start));
if (start == 0) {
SharedPreferences.Editor editor = settings.edit();
editor.putLong(FIRST_USE_DATE, now);
***editor.commit();***
}
}
Edit: I've just tried debugging your actual code and the same thing happened: the if statement it's always true and it gets made every time because there's no editor.commit() to save the changes to the FIRST_USE_DATE variable.
protected boolean isDemoExpired() {
Date d = new Date();
long now = d.getTime();
long demoStart;
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
demoStart = settings.getLong(FIRST_USE_DATE, 0);
if (demoStart == 0) {
SharedPreferences.Editor editor = settings.edit();
editor.putLong(FIRST_USE_DATE, now);
****editor.commit();****
System.out.println(Long.toString(demoStart));
return false;
}
return true;
}
Upvotes: 3
Reputation: 12780
You are making a mistake somewhere...
Are you sure you are not entering the if statement AND that start = 0 ?
Are you showing us the code you are running ?
You could execute following code (equivalent to your code)
public static void main(String[] args) {
long start = 0;
if (start == 0) {
System.out.println(Long.toString(start));
}
}
And you'd see you enter the if statement...
Upvotes: 0
Reputation: 43108
Why won't you use Long start instead of long and check for null value?
protected void foo() {
Date d = new Date();
long now = d.getTime();
Long start;
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
start = settings.getLong(FIRST_USE_DATE, 0);
Log.w(this.getClass().getName(), Long.toString(start));
if (start == null) {
SharedPreferences.Editor editor = settings.edit();
editor.putLong(FIRST_USE_DATE, now);
}
}
it seems that auto-unboxing is happening here and you get zero value. Another question is why 0 == 0 check doesn't pass.
Upvotes: 0