Hey guys, first time posting here so don't flame me for being new, I was just wondering if anyone could explain why my Arraylists are overwriting themselves. - public static void returnToBook(entry x){
-
addressBook.add(persevreIndex, x);
-
reversedBook.add(persevreIndex, x);
-
phoneBook.add(persevreIndex, x);
-
tempEntry = (entry) addressBook.get(persevreIndex);
-
temp = tempEntry.getName();
-
entryList.addElement(temp);
-
reversedList.addElement(temp);
-
phoneList.addElement(tempEntry.getPhoneNumber());
-
refreshzor();
-
revered();
-
phoneSort();
-
}
If you need more of the code please tell me, the bad news for me is that the zipped file containing all of my code is just to big to fit. I built the code in netbeans btw
8 4037
As I can see from the code you have provided, all is ok, you have added the entry. But maybe you are deleting the elements later on? Please provide code for "refreshzor()" and other functions you are calling later on.
Also it would be nice to have the code where the ArrayLists phoneBook etc. are defined. Maybe you are overwriting the reference to them somewhere?
And most important: put code statements everywhere in your code that print out the size of the ArrayLists. Then we can see where the elements are dereferenced/deleted.
Sorry about not being able to post the code earlier, was at school and can't use mediafire there, anyway here is the whole project, open the Ad dreesBookView and NewEntry classes in netbeans, those are the gui's and then the class I made myself for storing the entries is the entry class in the resources folder.
link: http://www.mediafire.com/
P.S. Sorry about how messy the code is, I am still learning how to program and this project is for my AP computer science class.
@royalaid
That is not the way to ask a question; post an sscce instead.
kind regards,
Jos
Honestly there is not much I can chop off that isn't going to affect the outcome, if you want I can highlight what i think the problem is but I don't know where it is coming from so that is why I posted all of the code, I am sorry if that is problem but I am still learning and not sure where or what to post. Also I switched the code so that it would store using a Array instead of a Arraylist, no change in the outcome of the results.
Refreshzor Method: - public static void refreshzor(){
-
Collections.sort(addressBook , new Comparator()
-
{
-
public int compare(Object o1, Object o2){
-
entry e1 = (entry) o1;
-
entry e2 = (entry) o2;
-
return e1.getName().compareToIgnoreCase(e2.getName());
-
}});
-
entryList.removeAllElements();
-
for (int x =0; x < addressBook.size(); x++){
-
tempEntry = (entry) addressBook.get(x);
-
temp = tempEntry.getName();
-
entryList.addElement(temp);
-
}
-
}
phoneSort and revereSort are Identical to this except that they use phoneBook and reverseBook.
Also the code used to add the enties: - private void addContact(java.awt.event.MouseEvent evt) {
-
temp = nameField.getText();
-
NoName NoName;
-
-
if(temp.equalsIgnoreCase("")){
-
JFrame mainFrame = AddressBookApp.getApplication().getMainFrame();
-
NoName = new NoName(mainFrame, true);
-
NoName.setLocationRelativeTo(mainFrame);
-
AddressBookApp.getApplication().show(NoName);}
-
else
-
newEntry.setName(temp);
-
-
temp = nickField.getText();
-
if(temp.equalsIgnoreCase("")){}
-
else
-
newEntry.setNick(temp);
-
-
temp = cityField.getText();
-
if(temp.equalsIgnoreCase("")){}
-
else
-
newEntry.setCity(temp);
-
-
temp = stateField.toString();
-
if(temp.equalsIgnoreCase(" ")){}
-
else
-
newEntry.setState(temp, stateField.getSelectedIndex());
-
-
temp = addressField.getText();
-
if(temp.equalsIgnoreCase("")){}
-
else
-
newEntry.setAddress(temp);
-
-
temp = emailField.getText();
-
if(temp.equalsIgnoreCase("")){}
-
else{
-
if (temp.indexOf('@') < 0 || temp.indexOf('.') < 0){
-
JFrame mainFrame = AddressBookApp.getApplication().getMainFrame();
-
UshallNotEmailFail = new UshallNotEmailFail(mainFrame, true);
-
UshallNotEmailFail.setLocationRelativeTo(mainFrame);
-
AddressBookApp.getApplication().show(UshallNotEmailFail);
-
}
-
else{
-
newEntry.setEmail(temp);
-
}
-
}
-
-
temp = zipCodeField.getText();
-
if(temp.equalsIgnoreCase(" ")){}
-
else
-
newEntry.setZip(temp);
-
-
temp = phoneNumberField.getText();
-
if(temp.equalsIgnoreCase("( ) - ")){}
-
else
-
newEntry.setPhone(temp);
-
-
temp = birthDayField.getText();
-
if (temp.equals(" / / ")){}
-
else
-
newEntry.setBirth(temp);
-
-
/*if (AddressBookView.editSwitchStatus() == true){
-
AddressBookView.returnToBook(newEntry);
-
AddressBookView.editSwitchOff();
-
}
-
else*/
-
AddressBookView.addToBook(newEntry);
-
-
-
temp = newEntry.getName();
-
if(temp.equalsIgnoreCase("")){}
-
else
-
setVisible(false);
Finally the entire entry class: - public class entry{
-
protected String name;
-
protected String nickName;
-
protected String city;
-
protected String state;
-
protected int stateIndex;
-
protected String address;
-
protected String zipCode;
-
protected String email;
-
protected String phoneNumber;
-
protected String birthyear;
-
protected String birthmonth;
-
protected String birthday;
-
-
public entry(String n,String nN, String bD, String a, String c, String s, int sI, String zC, String e, String pN) {
-
name = n;
-
nickName = nN;
-
city = c;
-
state = s;
-
stateIndex = sI;
-
address = a;
-
zipCode = zC;
-
email = e;
-
phoneNumber = pN;
-
birthday = bD;
-
}
-
-
public entry(){
-
name = "";
-
nickName = "";
-
city = "";
-
state = "";
-
stateIndex = 0;
-
address = "";
-
zipCode = "";
-
email = "";
-
phoneNumber = "";
-
birthday = "";
-
}
-
-
public void setName(String n){
-
name = n;
-
}
-
-
public void setNick(String nN){
-
nickName = nN;
-
}
-
-
public void setBirth(String bD){
-
birthday = bD;
-
}
-
-
public void setAddress(String a){
-
address = a;
-
}
-
-
public void setCity(String c){
-
city = c;
-
}
-
-
public void setState(String s, int sI){
-
state = s;
-
stateIndex = sI;
-
}
-
-
public void setZip(String zC){
-
zipCode = zC;
-
}
-
-
public void setPhone(String pN){
-
phoneNumber = pN;
-
}
-
-
public void setEmail(String e){
-
email = e;
-
}
-
-
public String getName(){
-
return name;
-
}
-
-
public String getAddress(){
-
return address;
-
}
-
-
public String getCity(){
-
return city;
-
}
-
-
public String getState(){
-
return state;
-
}
-
-
public int getStateIndex(){
-
return stateIndex;
-
}
-
-
public String getZipCode(){
-
return zipCode;
-
}
-
-
public String getPhoneNumber(){
-
return phoneNumber;
-
}
-
-
String getBirth() {
-
return birthday;
-
}
-
-
String getNick(){
-
return nickName;
-
}
-
-
String getEmail(){
-
return email;
-
}
-
}
Which is just a basic storage class
The problem is that you have global variables like "addressBook", "entryList" etc. that can affect each other everywhere. That's a bad programming practice and leads to the problem that you lose the overview where which variable is manipulated. Try to make your methods free of global variables, use only local ones. For example in your method "refreshzor", you are manipulating the global variable "entryList". First you add an element to the list in method "returnToBook" with "entryList.addElement(temp);". Then you call method "refreshzor", where you delete all elements of "entryList" with "entryList.removeAllElements(); ". So why add it first if you delete it right away? That's what I mean with side effects that global variables have. Try to think more object-orientated. What is the input, what is the output? A method like "refreshzor()" says nothing. But if I write "addressBook.sort(); tempEntry=copy(addressBook);" everybody understands: the object "addressBook" is sorted and then a copy is made and stored in tempEntry. And most important: no side effects. I can see directly which objects are manipulated.
If that's too much work for you, here is an example of what I mean for minimum changes: - public static void returnToBook(entry x){
-
addressBook.add(persevreIndex, x);
-
reversedBook.add(persevreIndex, x);
-
phoneBook.add(persevreIndex, x);
-
tempEntry = (entry) addressBook.get(persevreIndex);
-
temp = tempEntry.getName();
-
// entryList.addElement(temp); // commented out, because no effect
-
reversedList.addElement(temp);
-
phoneList.addElement(tempEntry.getPhoneNumber());
-
// refreshzor(); // old, deprecated function, do not use
-
entryList = refreshzor(addressBook); // here I can see that addressBook and entryList are changed.
-
revered();
-
phoneSort();
-
}
and your global-variables-free method: -
public static ArrayList refreshzor(ArrayList addressBook){
-
Collections.sort(addressBook , new Comparator()
-
{
-
public int compare(Object o1, Object o2){
-
entry e1 = (entry) o1;
-
entry e2 = (entry) o2;
-
return e1.getName().compareToIgnoreCase(e2.getName());
-
}});
-
ArrayList entryList = new ArrayList();
-
for (int x =0; x < addressBook.size(); x++){
-
tempEntry = (entry) addressBook.get(x);
-
temp = tempEntry.getName();
-
entryList.addElement(temp);
-
}
-
return entryList;
-
}
-
So, you have still not yet listed the code where the problem could be.
The code for functions "revered" and "phoneSort", that are called after "refreshzor", is still missing. Maybe you are manipulating global object again there, like "phoneList.removeAllElements()" or so ?. Also the code where "returnToBook" is called is missing, please list it here. It may be needed in the case that the analysis shows that your entries are not magically deleted inside your "returnToBook" function, that means the problem lies somewhere else outside.
To check for that I asked you kindly in my last forum posting to add code for printing out the list sizes and publish the output in your next posting. But you didn't do. Can you do it now, please? It would be a big help to detect the error!!! What I mean is, change your method "returnToBook" to: - public static void returnToBook(entry x){
-
addressBook.add(persevreIndex, x);
-
reversedBook.add(persevreIndex, x);
-
phoneBook.add(persevreIndex, x);
-
tempEntry = (entry) addressBook.get(persevreIndex);
-
temp = tempEntry.getName();
-
entryList.addElement(temp);
-
reversedList.addElement(temp);
-
phoneList.addElement(tempEntry.getPhoneNumber());
-
System.out.println("Step 1: size of addressBook=" + addressBook.size());
-
refreshzor();
-
System.out.println("Step 2: size of addressBook=" + addressBook.size());
-
revered();
-
System.out.println("Step 3: size of addressBook=" + addressBook.size());
-
phoneSort();
-
System.out.println("Step 4: size of addressBook=" + addressBook.size());
-
}
Of course you should add more statements the same way to also list the sizes for the other lists you have (that means for "reversedBook", "phoneBook", "reversedList", "entryList" and "phoneList")
@OP: also: perform some unit tests; call your methods in isolation and check whether or not each method does what it's supposed to do; showing us a lot of juggling with some ArrayLists and asking "what goes wrong?" doesn't bring you any help.
kind regards,
Jos
Ok I am going to try both of your suggestions, hopefully this will get me somewhere, and sorry about not posting the sizes of the arrays totally missed the fact you asked for that in your last post, makes me feel like more of a idiot. I should have that code up really soon if my teacher lets me work on it.
I figured out the problem, I wasn't instantiating a new entry when I added the contact so effectively my array was one giant pointer... anyway thanks for the help guys
Sign in to post your reply or Sign up for a free account.
Similar topics
by: daniel |
last post by:
I am working with a project which converts a java based application into C#. I noticed that when I converted the java file, .net throws a SupportClass for me and since there is no List type in C#,...
|
by: daniel li |
last post by:
I got another convert issue. This is the same project as I posted
yesterday, and received a perfect response.
The conversion threw a SupportClass for me and one of the data type is...
|
by: Xandau |
last post by:
hello all,
i wotk with java every day but last time i have interested in C#.
everything goes great except one thing...
in java everything is a reference (except plain types) so i thought
that...
|
by: Andrew Bell |
last post by:
I'm doing a uni course in the UK and one of my semesters is about
programming. This is just going to be compilied and executed with no menu
just using command promt (javac classfile.class)
I am...
|
by: Jobs |
last post by:
Download the JAVA , .NET and SQL Server interview with answers
Download the JAVA , .NET and SQL Server interview sheet and rate
yourself. This will help you judge yourself are you really worth of...
|
by: xavier vazquez |
last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
|
by: VeeraLakshmi |
last post by:
I am doing a project for internet control using Java,PHP and MySql.All sites
should go through the proxy server only.We are giving access rights as allow or
deny to the sites.If we type the...
|
by: chokcheese |
last post by:
I'm having trouble with a java application. When I try and run the program it shows a java.lang.NullPointerException in the "tic.getClient().getClientTicketList().add(tic);" line (it's in bold). I...
|
by: cowboyrocks2009 |
last post by:
Hi,
I need help to automate my code to take data from input file. Also I need to create it as a function so that I can pass it to some other program. I am new to Java so having a bit limitation to...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |