Serialization failed

Hello All,

I know a little about how Java's Serialization scheme works, but I am a bit confused as to why one of my classes throws the following exception:

IOException: java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: com.sm.objects.FileBundle

The class' code is as follows:

package com.sm.objects;

import java.io.File;

import java.io.Serializable;

import java.util.*;

import org.apache.log4j.Logger;

public class FileBundle implements Serializable {

/**

*

*/

private static final long serialVersionUID = 1460616094003861647L;

static Logger logger = Logger.getLogger(FileBundle.class);

private ArrayList<File> filesArray;

private String[] listOfFiles;

public FileBundle(){

logger.debug("New FileBundle of XML files has been created.");

filesArray = new ArrayList<File>();

}

public boolean addFile(File file){

boolean added = false;

if(filesArray.add(file)){

added = true;

logger.debug("Added " + file.toString() + " to FileBundle.");

}else{

logger.warn("File " + file.toString() + " not added.",

new Exception("FileBundle.addFile() failed."));

}

return added;

}

public void setListOfFiles(String[] listOfFiles){

this.listOfFiles = listOfFiles;

logger.debug("There are " + listOfFiles.length + " files in the bundle.");

}

public String[] getListOfFiles(){

logger.debug("retrieving " + listOfFiles.length + " files from the bundle.");

return listOfFiles;

}

public void setFiles(ArrayList<File> filesArray){

this.filesArray = filesArray;

logger.debug("Setting " + filesArray.size() + " files into the ArrayList.");

}

public ArrayList<File> getFiles(){

logger.debug("Counting " + filesArray.size() + " files from the ArrayList.");

return filesArray;

}

public int getNumberOfFiles(){

int i = 0;

if(filesArray.isEmpty()){

logger.debug("No files in FileBundle ArrayList.");

return i;

} else {

i = filesArray.size();

logger.debug( i + " files in FileBundle ArrayList.");

return i;

}

}

}

Might anyone have an idea, or point me in the right direction to solve my dilemma?

Thanks in advance!!

[2405 byte] By [TStrong_06a] at [2007-11-27 10:27:38]
# 1

Recompile it and retry.

ejpa at 2007-7-28 17:46:54 > top of Java-index,Java Essentials,Java Programming...
# 2

Just recompiled to no avail. I don't think I'm using any classes or anything that aren't Serializable.

Still at a loss as to what's not working...

TStrong_06a at 2007-7-28 17:46:54 > top of Java-index,Java Essentials,Java Programming...
# 3

I don't know what you are using to serialize with, but consider this assumption, the built in Java serializer will use the mutators, and accessor methods where available (getters and setters). Your accessor methods have log statements in them that assume the filesArray, and listOfFiles are not null. (I'm assuming there is some reason unknown to me that these are separated into two arrays, but that's not your question.)

Have you a.) guaranteed that those fields are not null before serializing as to not cause NullPointerException, and or b.) consider wrapping the logger in a conditional statement ,or using the ternary operator for the log, before starting the serialization process?

Try commenting your log statements out for a quick tests, and see if that fixes the problem.

Since the getters, and setters are public methods, and the constructor does nothing with those properties, I would at a minimum, fix that likely error even it is not the source of your problem.

Message was edited by:

dugrocker

dugrockera at 2007-7-28 17:46:54 > top of Java-index,Java Essentials,Java Programming...
# 4

> but consider this assumption, the built in Java

> serializer will use the mutators, and accessor

> methods where available (getters and setters).

This is untrue. The entire post can therefore be ignored.

ejpa at 2007-7-28 17:46:54 > top of Java-index,Java Essentials,Java Programming...
# 5

I had discounted the post as somewhat irrelevant to my issue. Bottom line is, I can't serialize this object, and I've been scouring my books and the API for a reason, only to find none...

TStrong_06a at 2007-7-28 17:46:54 > top of Java-index,Java Essentials,Java Programming...
# 6

Was there more to the exception error?

Where the stack trace?

Here's an idea. Make everything transient. Recompile retry.

Change one at a time till it works. How do we know, lol?

TuringPesta at 2007-7-28 17:46:54 > top of Java-index,Java Essentials,Java Programming...
# 7

I dont see that Logger implements Serializable.

Make it transient.

TuringPesta at 2007-7-28 17:46:54 > top of Java-index,Java Essentials,Java Programming...
# 8

> I dont see that Logger implements Serializable.

> Make it transient.

Isn't the logger static?

Hippolytea at 2007-7-28 17:46:54 > top of Java-index,Java Essentials,Java Programming...
# 9

I thought your spacebar didnt work. Shut up, lol. :)

TuringPesta at 2007-7-28 17:46:54 > top of Java-index,Java Essentials,Java Programming...