Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

FindBugs complaining about non-serializable field although everything looks Serializable

912 views
Skip to first unread message

laredo...@gmail.com

unread,
Jan 12, 2012, 11:40:38 AM1/12/12
to
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

Jeff Higgins

unread,
Jan 12, 2012, 12:04:52 PM1/12/12
to
On 01/12/2012 11:40 AM, laredo...@zipmail.com wrote:
> Hi,
>
> I'm using Java 1.6. My FindBugs tool is giving me this error ...
>
>
> Non-transient non-serializable instance field in serializable

?<http://findbugs.sourceforge.net/bugDescriptions.html#SE_BAD_FIELD>

laredo...@gmail.com

unread,
Jan 12, 2012, 12:48:34 PM1/12/12
to
On Jan 12, 11:04 am, Jeff Higgins <j...@invalid.invalid> wrote:
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

Daniel Pitts

unread,
Jan 12, 2012, 12:52:10 PM1/12/12
to
On 1/12/12 8:40 AM, laredo...@zipmail.com wrote:
> 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 {
<snip>
private List<Node> children;
> /**
> * 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.

Daniel Pitts

unread,
Jan 12, 2012, 12:52:42 PM1/12/12
to
Check again. java.util.List is not Serializable.

Ian Shef

unread,
Jan 12, 2012, 2:29:35 PM1/12/12
to
"laredo...@zipmail.com" <laredo...@gmail.com> wrote in
news:a3ed0747-5eb5-4da1...@v13g2000yqc.googlegroups.com:
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.

Lew

unread,
Jan 13, 2012, 12:10:28 AM1/13/12
to
Ian Shef wrote:
> List is NOT Serializable,

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.

--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

Lew

unread,
Jan 13, 2012, 12:11:14 AM1/13/12
to
On 01/12/2012 09:10 PM, Lew wrote:
> Ian Shef wrote:
>> List is NOT Serializable,
>
> 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)

Arne Vajhøj

unread,
Jan 14, 2012, 11:23:28 PM1/14/12
to
On 1/13/2012 12:10 AM, Lew wrote:
> Ian Shef wrote:
>> List is NOT Serializable,
>
> Isn't this something determined at runtime?

For serialization: yes.

For findbugs utility: no.

Arne

Lew

unread,
Jan 15, 2012, 1:18:04 PM1/15/12
to
Arne Vajhøj wrote:
> Lew wrote:
>> Ian Shef wrote:
>>> List is NOT Serializable,
>>
>> Isn't this something determined at runtime?
>
> For serialization: yes.
>
> For findbugs utility: no.

Hence the facepalm.
0 new messages