问题
I've got two classes right now: RemindersDAO.java and ViewLocalReminders.java.
I'm trying to get access to a variable that's in ViewLocalReminders.java and I'm trying to call it from RemindersDAO.java. I'm doing this by using the getter/setter method combo. However, for some reason, my variable value keeps getting set to 0 in the getter method. Here's the code:
ViewLocalReminders.java
public class ViewLocalReminders extends SherlockListActivity {
private long reminderID;
public void onCreate(Bundle SavedInstance) {
reminderID = 16;
setReminderID(reminderID);
}
public void setReminderID(long id) {
id = reminderID;
System.out.println("Reminder ID Value in Setter: " + reminderID);
}
public long getReminderID() {
return reminderID;
System.out.println("Reminder ID Value in Getter: " + reminderID);
}
}
RemindersDAO.java
public class RemindersDAO extends SQLiteOpenHelper {
@SuppressWarnings("deprecation")
public Cursor getRowByID(Activity activity) {
String[] from = { _ID, NAME };
ViewLocalReminders viewLocalReminders = new ViewLocalReminders();
long reminderID = viewLocalReminders.getReminderID();
System.out.println("Value of Reminder ID in RemindersDAO: " + reminderID);
}
}
This is the output that I'm getting for all my System.out.printlns:
- Reminder ID Value in Setter: 16
- Reminder ID Value in Getter: 0
- Reminder ID Value in RemindersDAO: 0
So obviously, something's happening between the setter and getter methods. The value of reminderID is 16 in the setter method, the way it should be. However, the value goes to 0 in the getter method. Consequently, the value is still 0 when I call the getter method in RemindersDAO.java. What am I doing wrong here? Thanks for your help! :)
回答1:
here:
public void setReminderID(long id) {
id = reminderID; //<-- wrong, should be the other way around
System.out.println("Reminder ID Value in Setter: " + reminderID);
}
You should use reminderID = id
instead
Also:
public void onCreate(Bundle SavedInstance) {
reminderID = 16; //one of these two lines..
setReminderID(reminderID); //..should do it
}
No need to set the reminderId
twice
回答2:
Your setter should look like this:
public void setReminderID(long id) {
reminderID = id;
System.out.println("Reminder ID Value in Setter: " + reminderID);
}
Your remainderID
variable should be on the left. The way you had it written, id was being set to reminderID.
Also, in your getter:
public long getReminderID() {
return reminderID;
System.out.println("Reminder ID Value in Getter: " + reminderID);
}
Since the System.out.println
is after the return, then you shouldn't ever see the output. Return will end the function right there.
回答3:
you have
id = remainderID;
it should be
remainderID = id;
回答4:
You're assignment of the reminder id is backwards
id = reminderID;
should read
reminderID = id;
in you setReminderID
method
回答5:
public void setReminderID(long id) {
this.reminderID = id;
System.out.println("Reminder ID Value in Setter: " + reminderID);
}
回答6:
try using this
this.remainderId = id;
instead of
remainderId = id;
in your model or domain.
回答7:
Your setter method seems wrong. You need to use your IDE's auto generation to create these methods.
回答8:
I see that you accepted the answer. However, I think I know the problem about your getter-setter bug. It's about the order and order matters...it's like you're giving the instruction step-by-step.
Let me explain...
The first one, your setter method, did a mistake. (See marked comment/s)
public void setReminderID(long id) {
id = reminderID; // --> your argument is id. reminderId field value should be changed not the other way around.
System.out.println("Reminder ID Value in Setter: " + reminderID);
}
Here's the correct setter.
public void setReminderID(long id) {
reminderID = id; // --> "id" is your parameter and that's the reminderID's new value.
System.out.println("Reminder ID Value in Setter: " + reminderID);
}
Next, about the getter. The reason why there's a bug in getter is because you have a wrong order. (See marked comment/s)
public long getReminderID() {
return reminderID;
System.out.println("Reminder ID Value in Getter: " + reminderID); // --> putting this line code after "return" results an error and therefore you won't be able to obtain a number or value!
}
Here's the correct getter. Look carefully the difference.
public long getReminderID() {
System.out.println("Reminder ID Value in Getter: " + reminderID); // --> Anycode before "return" value will be fine.
return reminderID; // --> Always put the "return" value at the last line within a getter method.
}
I hope you can use this correction for your future project in case whenever you need to apply the getter-setter method when you're programming. Remember that it's very important because ordering of the code matters and know where you direct the value changes. Questions?
回答9:
Thanks for all your help you guys. I wasn't able to figure out why my getters and setters weren't working, even though I followed all of your guys' suggestions. I'm guessing it has something to do with the fact that reminderID is taken in as an argument of a constructor. Either way, I solved my problem by using Android's Bundle and transferring my variables from one activity to another through that.
Again, thanks for all your help! :)
来源:https://stackoverflow.com/questions/12593490/getter-and-setters-not-working