Reputation: 14950
Good day!
I created overloading constructors as follows:
public ContactsBean(String firstName, String lastName,
String telNumber, String email) {
this.id = count;
count = count + 1;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.telNumber = telNumber;
}
public ContactsBean() {
this.id = count;
count = count + 1;
}
I want to auto-increment the id so i used this variables:
private static int count;
private int id;
My problem is, when I instantiate the ContactsBean() contacts = new ContactsBean()
, the value of id is incremented by 2..
2,4,6,8... etc.
Why? How can I do the auto number of ID increment by 1?
Thank you.
EDIT:
Action:
private ContactsBean contacts = new ContactsBean();
private ContactsManager contactsManager = new ContactsManager();
public String add() {
contactsManager.addContacts(contacts);
return SUCCESS;
}
Manager:
private ContactsDAO contactsDAO = ContactsDAO.getInstance();
private List<ContactsBean> contactsList = contactsDAO.getContactsList();
public void addContacts(ContactsBean contact) {
contactsList.add(contact);
}
Upvotes: 1
Views: 1733
Reputation: 14950
I learned that Struts2 generates the instance of an object(beans) automatically in the Action Classes so no need to instantiate it....
My code before is
private ContactsBean contacts = new ContactsBean();
private ContactsManager contactsManager = new ContactsManager();
public String add() {
contactsManager.addContacts(contacts);
return SUCCESS;
}
//getters and setters
I changed it to..
private ContactsBean contacts;
private ContactsManager contactsManager = new ContactsManager();
public String add() {
contactsManager.addContacts(contacts);
return SUCCESS;
}
//getters and setters
And it works...
Upvotes: 0
Reputation: 3196
My suggestion would be to not try to increment the contactID in the contructor, but either get it from the newly created database object where the ID is being incremented by databse via identity specification, or since you are getting the list of contacts base your next id off of contactsDAO.getContactsList().size()+1.
I'd also recommend changing from:
private ContactsDAO contactsDAO = ContactsDAO.getInstance();
private List<ContactsBean> contactsList = contactsDAO.getContactsList();
public void addContacts(ContactsBean contact) {
contactsList.add(contact);
}
To something like:
private ContactsDAO contactsDAO = ContactsDAO.getInstance();
private List<ContactsBean> contactsList;
public void addContacts(ContactsBean contact) {
int id = getContactList().size()+1;
contact.setId(id);
contactsList.add(contact);
}
public List<ContactsBean> getContactList(){
return contactsDAO.getContactsList();
}
alternatively if you are able to remove contacts from the database, this number might not be accurate for the ID. You could create a query based on such as:
select MAX(ID) from contacts
This will return the largest id number used.
Upvotes: 2
Reputation: 12344
It might be due to the copy constructor calling the no-arg version of your contstructor.
(Or am I suffering from C++ sickness?)
Upvotes: 2
Reputation: 21
Have you tried debugging your code setting a breakpoint on both constructors?
The suggestion of Eng. Fouad is a good tip but it's not gonna solve your problem.
Also note that your counter is not thread-safe (the problem has nothing to do with it, though. In that case your counter would have a lower value than it should)
And if you really need to keep track on how many objects you actually create, I don't think the best way to do this is with a static attribute in a Java Bean...
Upvotes: 2
Reputation: 13278
I think you are creating two objects of ContactsBean in your other classes may be you are unaware of it. You have to check the code.
Upvotes: 2
Reputation: 117607
private static int count = 0;
private int id;
// ...
public ContactsBean(String firstName, String lastName,String telNumber, String email)
{
this();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.telNumber = telNumber;
}
public ContactsBean()
{
id = ++count;
}
Upvotes: 0
Reputation: 29493
First, DRY (do not repeat yourself), would be better:
public ContactsBean(String firstName, String lastName,
String telNumber, String email){
this();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.telNumber = telNumber;
}
Second, there is no increment by two in your code. Please paste in your test code.
Upvotes: 5
Reputation: 21
Try removing the first set of parentheses from
ContactsBean() contacts = new ContactsBean();
.
That is, try this constructor:
ContactsBean contacts = new ContactsBean();
Upvotes: 2