FindBugs complaining about non-serializable field although everythinglooks Serializable

L

laredotornado

Hi,

I'm using Java 1.6. My FindBugs tool is giving me this error ...


Non-transient non-serializable instance field in serializable
class
Class com.myco.clearing.common.xml.Node defines non-transient non-
serializable instance field children


The class and its private fields that Findbugs is complaining about
are below ...


public class Node implements Serializable, Comparable<Node>,
Cloneable {
/**
* For serializable classes.
*/
private static final long serialVersionUID = 1L;

/**
* Unique id
*/
private long id;
/**
* Node Name
*/
private String name;
/**
* Node value
*/
private String value = "";
/**
* Child nodes
*/
private List<Node> children;
/**
* Parent node
*/
private Node parent;
/**
* Node attributes
*/
private List<Attribute> attributes;


I have a public, no-argument constructor and getter/setter methods for
all the fields you see (except serialVersionUID). Any ideas why
FindBugs is complaining about the field "children" or how I can
troubleshoot this further?

The above references a class, "Attribute". The relevant parts are
below. Same thing -- a public, no-argument constructor and getter/
setter methods present.


public class Attribute implements Serializable, Cloneable {

/**
* For serializable classes.
*/
private static final long serialVersionUID = 1L;

/**
* Attribute Name
*/
private String name;
/**
* Attribute value, can be local or inherited
*/
private String value;
/**
* Node having this attribute
*/
private Node node;


Thanks, - Dave
 
L

laredotornado

Hi, I read that, but all fields are serializable, including
java.util.List. So, I'm not seeing what is throwing it off, do you? -
Dave
 
D

Daniel Pitts

Hi,

I'm using Java 1.6. My FindBugs tool is giving me this error ...


Non-transient non-serializable instance field in serializable
class
Class com.myco.clearing.common.xml.Node defines non-transient non-
serializable instance field children


The class and its private fields that Findbugs is complaining about
are below ...


public class Node implements Serializable, Comparable<Node>,
Cloneable {
/**
* Parent node
*/
private Node parent;
/**
* Node attributes
*/
private List<Attribute> attributes;
I'm assuming that "List" is the java.util.List interface, which does not
extend Serializable. Many implementations *are* serializable, but the
interface itself is not. FindBugs is warning you that it is possible to
set the "children" and "attributes" fields to List implementations that
are not serializable.
 
D

Daniel Pitts

Hi, I read that, but all fields are serializable, including
java.util.List. So, I'm not seeing what is throwing it off, do you? -
Dave
Check again. java.util.List is not Serializable.
 
I

Ian Shef

Hi,

I'm using Java 1.6. My FindBugs tool is giving me this error ...


Non-transient non-serializable instance field in serializable
class
Class com.myco.clearing.common.xml.Node defines non-transient non-
serializable instance field children


The class and its private fields that Findbugs is complaining about
are below ...


public class Node implements Serializable, Comparable<Node>,
Cloneable {
/**
* For serializable classes.
*/
private static final long serialVersionUID = 1L;

/**
* Unique id
*/
private long id;
/**
* Node Name
*/
private String name;
/**
* Node value
*/
private String value = "";
/**
* Child nodes
*/
private List<Node> children;
/**
* Parent node
*/
private Node parent;
/**
* Node attributes
*/
private List<Attribute> attributes;


I have a public, no-argument constructor Not relevant.
and getter/setter methods for
all the fields you see (except serialVersionUID). Not relevant, I think.
Any ideas why
FindBugs is complaining about the field "children" or how I can
troubleshoot this further?

If I recall correctly, a class cannot be serialized unless all of its
fields are either Serializable or marked transient (or unless special
methods are written).

Primitives (long, in your casse) are always Serializable.
String is Serializable.
List is NOT Serializable, and you have not marked children and attributes
as transient. This will lead to a NotSerializableException at the time of
serialization.

Node cannot be serialized unless its nontransient fields can be serialized.
(Obviously, transient fields don't get serialized and don't get restored by
deserialization.)

<class Attribute snipped as not relevant>

The choices would seem to be:

Don't make Node Serializable, or
Mark children and attributes as transient, or
Use something Serializable in place of List (such as ArrayList), or
Provide writeObject and readObject methods (or writeReplace and
readResolve, or...) to appropriately serialize Node objects.
 
L

Lew

Isn't this something determined at runtime?

If the implementation type is, say, ArrayList, won't it just magically work?

Okay, okay, I'll run up NetBeans and see for myself.
Oh, wait, I see why I'm wrong. (facepalm)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top