mangstadt / ez-vcard

A vCard parser library for Java
Other
405 stars 93 forks source link

Iterator not iterating past 7th contact. #103

Closed IsDisDaPizza closed 4 years ago

IsDisDaPizza commented 4 years ago

I'm trying to temporarily reformat the contacts in a vcard to edit some data and then format it back as a vcard, however, when I try to iterate through the 226 contacts in my vcard file, the iterator stops at the 7th contact.

ArrayList<Contact> contacts = new ArrayList<Contact>();
        File file = new File(filename);
        List<VCard> vcards = Ezvcard.parse(file).all();
        System.out.println("Test?");
        Iterator<VCard> itr8 = vcards.iterator();
        Iterator<VCard> itr9 = vcards.iterator();
        int i = 0;
        int i2 = 0;
        while(itr9.hasNext()) { 
            i2++;
            System.out.println(i2);
            itr9.next();
        }

        try {

            while(i<i2) {
                    Contact contact = new Contact(); //temporary contact that will be appended to the list is created here
                    System.out.println(itr8.next().getTelephoneNumbers());
                    //This (^^^) bit used to be used to check whether or not the code was properly reading the numbers.     
                    //The ensuing code is an agglomeration of try{}catch(){} to read the contact. They appear to be the only way of reading the contact with no errors. I did try to pair this with a series of Ternary operations, but they ended up just messing with the code, so I commented them out

                    //////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.ADR = itr8.next().getAddresses(); // != null ? itr8.next().getAddresses() : contact.ADR;
                    }
                    catch(Exception e) {
                        System.out.println("Adresses not Found");
                    }
                    //////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.AGN = itr8.next().getAgent(); //!= null ? itr8.next().getAgent() : contact.AGN;
                    }
                    catch(Exception e) {
                        System.out.println("Agent not found");
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.ann = itr8.next().getAnniversary(); //!= null ? itr8.next().getAnniversary() : contact.ann;
                    }
                    catch(Exception e) {
                        System.out.println("Anniversary not found");
                    }
                    ////////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.bday = itr8.next().getBirthday(); //!= null ? itr8.next().getBirthday() : contact.bday;
                    }
                    catch(Exception e) {
                        System.out.println("Birthday not found");
                    }
                    /////////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.ded = itr8.next().getDeathdate(); // != null ? itr8.next().getDeathdate() : contact.ded;
                    }
                    catch(Exception e) {
                        System.out.println("Death Date not found");
                    }
                    ////////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.dedp = itr8.next().getDeathplace(); // != null ? itr8.next().getDeathplace() : contact.dedp;
                    }
                    catch(Exception e){
                        System.out.println("Death Place not found");
                    }
                    ///////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.exp = itr8.next().getExpertise(); // != null ? itr8.next().getExpertise() : contact.exp;
                    }
                    catch(Exception e) {
                        System.out.println("No Expertise Found");
                    }
                    //////////////////////////////////
                    try { //I cannot remove this option, even if it frees up all the memory
                        contact.fname = itr8.next().getFormattedName(); // != null ? itr8.next().getFormattedName() : contact.fname;
                    }
                    catch(Exception e) {
                        System.out.println("Formatted Name not Found");
                    }
                    //////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.gender = itr8.next().getGender(); // != null ? itr8.next().getGender() : contact.gender;
                    }
                    catch(Exception e) {
                        System.out.println("Gender not found");
                    }
                    //////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.geo = itr8.next().getGeo(); // != null ? itr8.next().getGeo() : contact.geo;
                    }
                    catch(Exception e) {
                        System.out.println("Geo not found");
                    }
                    ////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.hobby = itr8.next().getHobbies(); // != null ? itr8.next().getHobbies() : contact.hobby; 
                    }
                    catch(Exception e) {
                        System.out.println("Hobbies not found :'v");
                    }
                    ////////////////////////////////
                    try{ //I understand this is crucial for Vcard contacts, So I cannot remove it either
                        contact.ID = itr8.next().getUid(); // != null ? itr8.next().getUid() : contact.ID;
                    }
                    catch(Exception e) {
                        System.out.println("UID not found");
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.impp = itr8.next().getImpps(); // != null ? itr8.next().getImpps() : contact.impp;
                    }
                    catch(Exception e) {
                        System.out.println("IMPPs not found");
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.interest = itr8.next().getInterests(); // != null ? itr8.next().getInterests() : contact.interest;
                    }
                    catch(Exception e) {
                        System.out.println("Interests not found");
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.key = itr8.next().getKeys(); // != null ? itr8.next().getKeys() : contact.key;
                    }
                    catch(Exception e) {
                        System.out.println("Keys not found");
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.kind = itr8.next().getKind(); // != null ? itr8.next().getKind() : contact.kind;
                    }
                    catch(Exception e) {
                        System.out.println("Kind not found... I guess you could say, 'we are not your kind' :V");
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.lang = itr8.next().getLanguages(); // != null ? itr8.next().getLanguages() : contact.lang;
                    }
                    catch(Exception e) {
                        System.out.println("Languages not found"); 
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.logo = itr8.next().getLogos(); // != null ? itr8.next().getLogos() : contact.logo;
                    }
                    catch(Exception e) {
                        System.out.println("Logos not found");
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.mail = itr8.next().getEmails(); // != null ? itr8.next().getEmails() : contact.mail;    
                    }
                    catch(Exception e) {
                        System.out.println("Emails not found");
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.mailer = itr8.next().getMailer(); // != null ? itr8.next().getMailer() : contact.mailer;
                    }
                    catch(Exception e) {
                        System.out.println("Mailer not found");
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts, and I'd rahter not remove Nickname
                        contact.nick = itr8.next().getNickname(); // != null ? itr8.next().getNickname() : contact.nick;
                    }
                    catch(Exception e) {
                        System.out.println("Nickname not found");
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.note = itr8.next().getNotes(); // != null ? itr8.next().getNotes() : contact.note;                  
                    }
                    catch(Exception e) {
                        System.out.println("Notes not found");
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.org = itr8.next().getOrganization(); // != null ? itr8.next().getOrganization() : contact.org;
                    }
                    catch(Exception e) {
                        System.out.println("Org not found");
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.photo = itr8.next().getPhotos(); // != null ? itr8.next().getPhotos() : contact.photo;              
                    }
                    catch(Exception e) {
                        System.out.println("photos not found");
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.POB = itr8.next().getBirthplace(); //!= null ? itr8.next().getBirthplace() : contact.POB;
                    }
                    catch(Exception e) {
                        System.out.println("Birthplace not found");
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.profile = itr8.next().getProfile(); // != null ? itr8.next().getProfile() : contact.profile;
                    }
                    catch(Exception e) {
                        System.out.println("Profile not found");
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.relation = itr8.next().getRelations(); // != null ? itr8.next().getRelations() : contact.relation;
                    }
                    catch(Exception e) {
                        System.out.println("Relations not found");
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.REV = itr8.next().getRevision(); // != null ? itr8.next().getRevision() : contact.REV;
                    }
                    catch(Exception e) {
                        System.out.println("Revisions not found");
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.role = itr8.next().getRoles(); // != null ? itr8.next().getRoles() : contact.role;
                    }
                    catch(Exception e) {
                        System.out.println("Roles not found");
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.sound = itr8.next().getSounds(); // != null ? itr8.next().getSounds() : contact.sound;
                    }
                    catch(Exception e) {
                        System.out.println("Sounds not found");
                    }
                    /////////////////////////////////
                    try { //Cannot Remove this 
                        contact.sname = itr8.next().getStructuredName(); // != null ? itr8.next().getStructuredName() : contact.sname;
                    }
                    catch(Exception e) {
                        System.out.println("Structured Name not found");
                    }
                    /////////////////////////////////
                    try { //Cannot remove this, as it is crucial to my project
                        contact.tel = itr8.next().getTelephoneNumbers(); // != null ? itr8.next().getTelephoneNumbers() : contact.tel;
                    }
                    catch(Exception e) {
                        System.out.println("Phone numbers not found... ok then... ");
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.title = itr8.next().getTitles(); // != null ? itr8.next().getTitles() : contact.title;
                    }
                    catch(Exception e) {
                        System.out.println("Titles not found");
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.tz = itr8.next().getTimezone(); // != null ? itr8.next().getTimezone() : contact.tz;
                    }
                    catch(Exception e) {
                        System.out.println("Timezone(s) not found");
                    }
                    /////////////////////////////////
                    try { //Removing this option makes space for 0 contacts
                        contact.URL = itr8.next().getUrls(); // != null ? itr8.next().getUrls() : contact.URL;
                    }
                    catch(Exception e) {
                        System.out.println("URLs not found");
                    }
                    ///////////////////////////////// these / comments were for me to easily be able to identify where a try{}catch(){} started and when it ended
                    //After systematically removing all plausible options one by one, I've concluded the issue is not memory
                    i++;
                    contacts.add(contact);
                    System.out.println("Contact " + i + " listed"); //here to see if all my contacts have properly been apended to the list

            }
        }
        catch(Exception IndexOutOfBoundException) {
            System.out.println("Whoop. Something went wrong. Contact " + i + " broke the code");
        }

I used two iterators and while functions because I wanted to make sure the iterator went through all the contacts when I didn't write it into my list, they do, so there's something in the second, really large while Loop, I just don't know what the problem is. I tried systematically deleting every option but it didn't work. Also, for context, a "Contact" object is just an object that contains all the features of a VCard object, I just wanted to put the VCard in a format more familiar to myself. If there's more information needed, I'm happy to oblige, I'll continue looking for solutions meanwhile

IsDisDaPizza commented 4 years ago

I originally thought it might be a memory issue, since randomly removing a couple of items iterated 9 contacts, but after systematically removing items, I saw that wasn't the case

IsDisDaPizza commented 4 years ago

well I'm stupid, I figured it out on my own. Sorry for bothering