1

This is related to Array of objects in a class. I originally added it on to that question but because that question was answered I wasn't getting responses.

So I have 2 classes. One containing a list of the other class. And I am adding data into the class that I have pulled out of the database. However when I go to pull the data back out of the List, values are all the same. They are the values of the last element in the list.

I have tried creating the object anew in each loop but it still seems to be causing a problem. I don't see what I am doing wrong here.

public class xmldata {
    String Barcode;
    String First;
    String Last;
    String Phone;
    String Email;

    String md5sum;

    String zipfile;

    List<PictureData> pics = new ArrayList<PictureData>();

...

public class PictureData {

    static String filename;
    static String directory;

...

xmldata data = new xmldata();

ResultSet pictures=db.query("select * from pictures where barcode=?",barcode);



while (pictures.next()) {
    PictureData pictemp= new PictureData();
    pictemp.setdirectory(pictures.getString("path"));
    pictemp.setfilename(pictures.getString("filename"));
    data.pics.add(pictemp);     
} 

...

for (int j=0; j<data.pics.size();++j) {

    String path;
    PictureData pictemp2= new PictureData();

    pictemp2=(PictureData) data.pics.get(j);
    path=pictemp2.getdirectory()+pictemp2.getfilename();

    System.out.println(path);

    zip.addfile(path);

}
Community
  • 1
  • 1
Codeguy007
  • 891
  • 1
  • 12
  • 32

1 Answers1

5

Problem is here:

public class PictureData {

    static String filename;
    static String directory;

}

Why have you made filename and directory static? it can contain only one value. Make them non static and it will work.

Lokesh
  • 7,810
  • 6
  • 48
  • 78