I have created a function that is supposed to go through a text file (csv) and read the information in it and then add it do a dataobject (which just holds the info in textfields and a combobox) which when filled adds itself to the jpanel. The issue I am having is when it adds to the jpanel I want to add the dataobject to an arraylist (for future saving)

The ArrayList is giving me issues on what it is saving/adding. I have to clear the arraylist so that I don't end up with duplicate information, but when I clear the arraylist, it clears that arraylist in my database object. I believe I'm just over thinking the issue at hand and figured I would post it here to see if someone can pick up on what I'm missing.

public void ReadFile() {	

        String one, two, three, four, five, six, data1, data2, data3, data4, data5, data6, data7, data8, data9, data10;
        ArrayList allData = new ArrayList(); 
        ArrayList<DataObject> list = new ArrayList<DataObject>();

        try {
            com.csvreader.CsvReader reader = new CsvReader(CsvPath);
            reader.readHeaders();
            while(reader.readRecord()) {

                //get the data from file
                one = reader.get("Info One");
                two = reader.get("Info Two");
                three = reader.get("Info Three");
                four = reader.get("Info Four");
                five = reader.get("Info Five");
                six = reader.get("Info Six");
                data1 = reader.get("Data#1");
                data2 = reader.get("Data#2");
                data3 = reader.get("Data#3");
                data4 = reader.get("Data#4");
                data5 = reader.get("Data#5");
                data6 = reader.get("Data#6");
                data7 = reader.get("Data#7");
                data8 = reader.get("Data#8");
                data9 = reader.get("Data#9");
                data10 = reader.get("Data#10");

                //add data to array
                allData.add(data1);
                allData.add(data2);
                allData.add(data3);
                allData.add(data4);
                allData.add(data5);
                allData.add(data6);
                allData.add(data7);
                allData.add(data8);
                allData.add(data9);
                allData.add(data10);

                //grab the info and put it in a new database object
                DatabaseObject ndbo = new DatabaseObject(one, two, three, four, five, six, allDccs);

                //grab info from database object and store for writing to file
                list.add(ndbo);

                //clear ArrayList allDccs
                //issues with arraylist, it clears completely leaving the databaseobject without info
                allDccs.clear();

            }

            //close reader
            reader.close();

            //iterate through and add DatabaseObjects to jPanel
            Iterator<DatabaseObject> iter = list.iterator();
            while(iter.hasNext()) {
                DatabaseObject ndbo = iter.next();
                row++; //increment row by 1
                constraints(row, ndbo, jPanel3); //add the databaseobject to the jPanel
            }
            System.out.println(list);

        } catch(Exception e) {
            e.printStackTrace();
        }
    }

Output:

[ID=1,Drive1,User1,Dot1,Float1,Int1,[],
ID=2,Drive2,User2,Dot2,Float2,Int2,[],
ID=3,Drive3,User3,Dot3,Float3,Int3,[],
ID=4,Drive4,User4,Dot4,Float4,Int4,[],
ID=5,Drive5,User5,Dot5,Float5,Int5,[],
ID=6,Drive6,User6,Dot6,Float6,Int6,[],
ID=7,Drive7,User7,Dot7,Float7,Int7,[],
ID=8,Drive8,User8,Dot8,Float8,Int8,[],
ID=9,Drive9,User9,Dot9,Float9,Int9,[]]

Since I clear the ArrayList allData it leaves that field in my DatabaseObject blank. But I clear it because I don't want any old data going into a different DatabaseObject.


Desired Output:

[ID=1,Drive1,User1,Dot1,Float1,Int1,[Date1, Date2, Date3, Date4, Date5, Date6, Date7, Date8, Date9, Date10],
ID=2,Drive2,User2,Dot2,Float2,Int2,[Girl1, Girl2, Girl3, Girl4, Girl5, Girl6, Girl7, Girl8, Girl9, Girl10],
ID=3,Drive3,User3,Dot3,Float3,Int3,[Boy1, Boy2, Boy3, Boy4, Boy5, Boy6, Boy7, Boy8, Boy9, Boy10],
ID=4,Drive4,User4,Dot4,Float4,Int4,[Couple1, Coupl2, Coupl3, Coupl4, Couple5, Couple6, Couple7, Couple8, Couple9, Couple10],
ID=5,Drive5,User5,Dot5,Float5,Int5,[Day1, Day2, Day3, Day4, Day5, Day6, Day7, Day8, Day9, Day10],
ID=6,Drive6,User6,Dot6,Float6,Int6,[Week1, Week2, Week3, Weel4, Week5, Week6, Week7, Week8, Week9, Week10],
ID=7,Drive7,User7,Dot7,Float7,Int7,[Hour1, Hour2, Hour3, Hour4, Hour5, Hour6, Hour7, Hour8, Hour9, Hour10],
ID=8,Drive8,User8,Dot8,Float8,Int8,[Min1, Min2, Min3, Min4, Min5, Min6, Min7, Min8, Min9, Min10],
ID=9,Drive9,User9,Dot9,Float9,Int9,[Time1, Time2, Time3, Time4, Time5, Time6, Time7, Time8, Time9, Time10]]

As you can see the desired output has the info that I enter into the arraylist for the comboboxes


Also, if you need me to post the DatabaseObject source I can do that, just state so.

I appreciate any and all help

Recommended Answers

All 2 Replies

Initialize allData or allDccs (I guess there is some sort of mix-up here) in the while loop and you should be good to go without having to clear the list.

Anyways, to clarify up the matter, there is only one list in your case which is pointed to or referred by two different references. So when you say you clear one of those lists, you are actually using one of those references to clear the only list you have and hence it is giving you issues.

commented: Thank you for the help and explanation of why it was going wrong. It helps to understand how/why code isn't working the way I intended it to. Cheers bud +1

Initialize allData or allDccs (I guess there is some sort of mix-up here) in the while loop and you should be good to go without having to clear the list.

Anyways, to clarify up the matter, there is only one list in your case which is pointed to or referred by two different references. So when you say you clear one of those lists, you are actually using one of those references to clear the only list you have and hence it is giving you issues.

My apologies, I was changing the ArrayList from allDccs to allData right before I posted it which is why there was a mix up.

You are correct, initializing the ArrayList in the while loop was what I needed to do. I guess I got caught up thinking I would have to clear the array no matter what that I didn't even think that it would create a new ArrayList each time that it looped.

Thank you for the help bud, much appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.