issues with duplicate value

I have a function that searches a folder for images, and then gives the images a name, and then calls a function that sends them to a server. for some reason it continues to keep the same name when i call the contact.setImageName()..here is my code and a sample output of 4 images entered

publicvoid updateContactImage(){

if (currentC_ID == 0){

JOptionPane.showMessageDialog(null,"Error - Please Enter or Select a Contact","Error", JOptionPane.DEFAULT_OPTION);

}else{

String fileName ="";

String inputValue ="";

String path ="C:\\Images\\";

boolean exists = (new File(path)).exists();

if (exists){

File folder =new File(path);

File[] listOfFiles = folder.listFiles();

if (listOfFiles.length >0){

for (int i = 0; i < listOfFiles.length; i++){

if (listOfFiles[i].isFile()){

fileName = listOfFiles[i].getAbsolutePath();

if (fileName.endsWith(".jpeg") || fileName.endsWith(".jpg") || fileName.endsWith(".JPG") || fileName.endsWith(".JPEG")){// || fileName.endsWith(".avi")){

try{

FileInputStream input =new FileInputStream(new File(fileName));

ByteArrayOutputStream output =new ByteArrayOutputStream();

byte[] buf =newbyte[1024];

byte[] data =null;

int numBytesRead = 0;

while ((numBytesRead = input.read(buf)) != -1){

output.write(buf, 0, numBytesRead);

}

contact =new contactObject();

System.out.println("contact.getImageName()= " + contact.getImageName());

System.out.println("inputeValue = " + inputValue);

contact.setAction("updateImage");

contact.setP_ID(currentC_ID);

inputValue = JOptionPane.showInputDialog("Input name for image:");

System.out.println("inputValue = " + inputValue +" time = " + getTime());

contact.setImageName(inputValue +" " + getTime());

contact.setImage(output.toByteArray());

System.out.println("Image file name = " + contact.getImageName());

sendContact();

output.close();

input.close();

}catch (FileNotFoundException ex){

ex.printStackTrace();

}catch (IOException ex){

ex.printStackTrace();

}

}

}elseif (listOfFiles[i].isDirectory()){

System.out.println("Directory " + listOfFiles[i].getName());

}

}

}else{

JOptionPane.showMessageDialog(null,"There are no files to submit");

}

}else{

System.out.println("directory does not exist");

}

}

contact =new contactObject();

}

contact.getImageName()= null

inputeValue =

inputValue = 1 time = 04-13-2007-06-33-12

Image file name = 1 04-13-2007-06-33-12

contact.getImageName()= null

inputeValue = 1

inputValue = 2 time = 04-13-2007-06-33-13

Image file name = 1 04-13-2007-06-33-12

contact.getImageName()= null

inputeValue = 2

inputValue = 3 time = 04-13-2007-06-33-16

Image file name = 1 04-13-2007-06-33-12

contact.getImageName()= null

inputeValue = 3

inputValue = 4 time = 04-13-2007-06-33-17

Image file name = 1 04-13-2007-06-33-12

for some reason when setting the contact.setImageName() it keeps the value of the previous one. here is my code for contact.setImageName()

public void setImageName(String newImage){

imageName[imageNameCount] = newImage;

imageNameCount++;

}

thanks for any help

[5850 byte] By [developprogramsa] at [2007-11-27 0:57:45]
# 1

Don't know but as a first step why don't you try adding some print statements to see what's happing in setImageName to confirm it's working as you expect.

public void setImageName(String newImage){

System.out.println("newImage = " + newImage);

imageName[imageNameCount] = newImage;

System.out.println("imageNameCount: " + imageNameCount + "imageName[imageNameCount] = " + imageName[imageNameCount]);

imageNameCount++;

}

CSAngela at 2007-7-11 23:31:22 > top of Java-index,Java Essentials,New To Java...
# 2
i put in the sout in the setImageName() and it prints out the value that i enter.
developprogramsa at 2007-7-11 23:31:23 > top of Java-index,Java Essentials,New To Java...
# 3
> i put in the sout in the setImageName() and it prints> out the value that i enter.Perfect, is the image count incrementing properly as well?
CSAngela at 2007-7-11 23:31:23 > top of Java-index,Java Essentials,New To Java...
# 4

i added a contact.imageNameCount = 0; and contact.count = 0; and this solved my problem. due to the array in the object it was keeping the count in the object when i thought it was being reset when it started a new object so it now looks like

public void updateContactImage(){

if (currentC_ID == 0){

JOptionPane.showMessageDialog(null,"Error - Please Enter or Select a Contact", "Error", JOptionPane.DEFAULT_OPTION);

} else {

String fileName = "";

String inputValue = "";

String path = "C:\\Images\\";

boolean exists = (new File(path)).exists();

if (exists) {

File folder = new File(path);

File[] listOfFiles = folder.listFiles();

if (listOfFiles.length >0){

for (int i = 0; i < listOfFiles.length; i++) {

if (listOfFiles[i].isFile()) {

fileName = listOfFiles[i].getAbsolutePath();

if (fileName.endsWith(".jpeg") || fileName.endsWith(".jpg") || fileName.endsWith(".JPG") || fileName.endsWith(".JPEG")){// || fileName.endsWith(".avi")){

try {

FileInputStream input = new FileInputStream(new File(fileName));

ByteArrayOutputStream output = new ByteArrayOutputStream();

byte[] buf = new byte[1024];

byte[] data = null;

contact.count = 0;

contact.imageNameCount = 0;

int numBytesRead = 0;

while ((numBytesRead = input.read(buf)) != -1) {

output.write(buf, 0, numBytesRead);

}

contact = new contactObject();

System.out.println("contact.getImageName()= " + contact.getImageName());

System.out.println("inputeValue = " + inputValue);

contact.setAction("updateImage");

contact.setP_ID(currentC_ID);

inputValue = JOptionPane.showInputDialog("Input name for image:");

System.out.println("inputValue = " + inputValue + " time = " + getTime());

contact.setImageName(inputValue + " " + getTime());

contact.setImage(output.toByteArray());

System.out.println("Image file name = " + contact.getImageName());

sendContact();

output.close();

input.close();

} catch (FileNotFoundException ex) {

ex.printStackTrace();

} catch (IOException ex) {

ex.printStackTrace();

}

}

} else if (listOfFiles[i].isDirectory()) {

System.out.println("Directory " + listOfFiles[i].getName());

}

}

}else {

JOptionPane.showMessageDialog(null, "There are no files to submit");

}

}else{

System.out.println("directory does not exist");

}

}

contact = new contactObject();

}

developprogramsa at 2007-7-11 23:31:23 > top of Java-index,Java Essentials,New To Java...
# 5

Hmmm. I don't think you need to do that. Are your count variables declared as static? They shouldn't be if you want them to be different for each contact object.

Make sure they're not static AND that they are initialized to 0 when a new object is created and you should be fine.

If you intend to leave it as is, I would create a contact.reset() method that sets the counters to 0 instead of accessing them directly. They should be private to your Contact class.

CSAngela at 2007-7-11 23:31:23 > top of Java-index,Java Essentials,New To Java...