Object not saving in Array of Objects....


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Object not saving in Array of Objects....

  1. #1
    Join Date
    Feb 2006
    Posts
    1

    Object not saving in Array of Objects....

    well...my problem is im storing values from a file into a regular object called Students. I then take that information and assign it to an array of Student objects.


    try
    {
    fileReaderIn = new FileReader("A2Q1in.txt");
    fileIn = new BufferedReader(fileReaderIn);

    testLine = fileIn.readLine();
    while(testLine != null)
    {
    String[] result = testLine.split("\\s+");

    //im previously aware of what the input file looks like
    students1.setALL(result[1], result[0], result[2]);
    students1.setGPA(Double.parseDouble(result[3]));


    students4[counter] = students1;//if i test this, i find that the values are being store into the array
    //-----------------------------------------------------
    System.out.println("Counter----> " + counter);
    counter++;
    testLine = fileIn.readLine();
    }
    fileIn.close();
    }
    catch (IOException ioe)
    {
    System.out.println(ioe.getMessage());
    ioe.printStackTrace();
    }
    //-----------------------------------------------------

    System.out.println("counter ---> " + counter);
    students = new Student[counter];
    for(int j = 0; j < counter; j++)
    {
    students[j] = students4[j];// printing this will print the same person from each entry
    }

    return students;

    now somewhere between the dashed lines, all of my values in my array of objects become one and the same, and whether by coincience or logic problems they all are the last bit of info retrived from the file.

    im not sure what it is im doing wrong but if anyone could help me with arrays of objects that would be much appreciated. Thx

  2. #2
    Join Date
    Jan 2006
    Posts
    53
    Code:
    students4[counter] = students1;
    With this line you are setting every element in students 4 to point to student1. There is only one student1, whos values you keep changing, but which is pointed to by all elements of the student4 array.

    Each element of student4 should point to a different student object. One way to do this is create a new student object for each pass through your readLine() loop.

    Remember, an array is a collection of object references. You can make them all point to the same object if you wish, but it's more useful here if they each point to a different student object.

Similar Threads

  1. Saving a variable as a reference in an object
    By Christopher in forum .NET
    Replies: 1
    Last Post: 07-09-2003, 09:24 AM
  2. Pooling in n-tier architecture
    By Jeff Pipes in forum .NET
    Replies: 11
    Last Post: 05-15-2002, 10:17 AM
  3. Even C++ had this much right...
    By Derek Mooney in forum .NET
    Replies: 94
    Last Post: 10-29-2001, 08:44 PM
  4. Can there be an Image object array?
    By Nathan Moore in forum Web
    Replies: 0
    Last Post: 05-09-2001, 01:15 PM
  5. JTable from array of Objects
    By styles in forum Java
    Replies: 2
    Last Post: 04-17-2001, 04:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center