FindBugs complaining about non-serializable field although everything looks Serializable

  • Follow


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
0
Reply laredotornado (853) 1/12/2012 4:40:38 PM

On 01/12/2012 11:40 AM, laredotornado@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>

> 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

0
Reply jeff8956 (433) 1/12/2012 5:04:52 PM


On Jan 12, 11:04=A0am, Jeff Higgins <j...@invalid.invalid> wrote:
> On 01/12/2012 11:40 AM, laredotorn...@zipmail.com wrote:
>
> > Hi,
>
> > I'm using Java 1.6. =A0My FindBugs tool is giving me this error ...
>
> > =A0 =A0 =A0Non-transient non-serializable instance field in serializabl=
e
>
> ?<http://findbugs.sourceforge.net/bugDescriptions.html#SE_BAD_FIELD>
>
>
>
>
>
>
>
> > class
> > =A0 =A0 =A0Class com.myco.clearing.common.xml.Node defines non-transien=
t non-
> > serializable instance field children
>
> > The class and its private fields that Findbugs is complaining about
> > are below ...
>
> > =A0 =A0 =A0public class Node implements Serializable, Comparable<Node>,
> > Cloneable {
> > =A0 =A0/**
> > =A0 =A0 * For serializable classes.
> > =A0 =A0 */
> > =A0 =A0private static final long serialVersionUID =3D 1L;
>
> > =A0 =A0/**
> > =A0 =A0 * Unique id
> > =A0 =A0 */
> > =A0 =A0private long id;
> > =A0 =A0/**
> > =A0 =A0 * Node Name
> > =A0 =A0 */
> > =A0 =A0private String name;
> > =A0 =A0/**
> > =A0 =A0 * Node value
> > =A0 =A0 */
> > =A0 =A0private String value =3D "";
> > =A0 =A0/**
> > =A0 =A0 * Child nodes
> > =A0 =A0 */
> > =A0 =A0private List<Node> =A0children;
> > =A0 =A0/**
> > =A0 =A0 * Parent node
> > =A0 =A0 */
> > =A0 =A0private Node parent;
> > =A0 =A0/**
> > =A0 =A0 * Node attributes
> > =A0 =A0 */
> > =A0 =A0private List<Attribute> =A0attributes;
>
> > I have a public, no-argument constructor and getter/setter methods for
> > all the fields you see (except serialVersionUID). =A0Any ideas why
> > FindBugs is complaining about the field "children" or how I can
> > troubleshoot this further?
>
> > The above references a class, "Attribute". =A0The relevant parts are
> > below. =A0Same thing -- a public, no-argument constructor and getter/
> > setter methods present.
>
> > =A0 =A0 =A0public class Attribute implements Serializable, Cloneable {
>
> > =A0 =A0/**
> > =A0 =A0 * For serializable classes.
> > =A0 =A0 */
> > =A0 =A0private static final long serialVersionUID =3D 1L;
>
> > =A0 =A0/**
> > =A0 =A0 * Attribute Name
> > =A0 =A0 */
> > =A0 =A0private String name;
> > =A0 =A0/**
> > =A0 =A0 * Attribute value, can be local or inherited
> > =A0 =A0 */
> > =A0 =A0private String value;
> > =A0 =A0/**
> > =A0 =A0 * Node having this attribute
> > =A0 =A0 */
> > =A0 =A0private Node node;
>
> > Thanks, - Dave

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
0
Reply laredotornado (853) 1/12/2012 5:48:34 PM

On 1/12/12 8:40 AM, laredotornado@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.

0
Reply newsgroup.nospam (530) 1/12/2012 5:52:10 PM

On 1/12/12 9:48 AM, laredotornado@zipmail.com wrote:
> On Jan 12, 11:04 am, Jeff Higgins<j...@invalid.invalid>  wrote:
>> On 01/12/2012 11:40 AM, laredotorn...@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>
>>
>>
>>
>>
>>
>>
>>
>>> 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
>
> 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.
0
Reply newsgroup.nospam (530) 1/12/2012 5:52:42 PM

"laredotornado@zipmail.com" <laredotornado@gmail.com> wrote in 
news:a3ed0747-5eb5-4da1-9f7f-c6ebdc8cbafd@v13g2000yqc.googlegroups.com:

> 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.
0
Reply invalid62 (88) 1/12/2012 7:29:35 PM

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
0
Reply noone7 (3512) 1/13/2012 5:10:28 AM

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)

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
0
Reply noone7 (3512) 1/13/2012 5:11:14 AM

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

0
Reply arne6 (9481) 1/15/2012 4:23:28 AM

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.

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
0
Reply noone7 (3512) 1/15/2012 6:18:04 PM

9 Replies
58 Views

(page loaded in 0.211 seconds)


Reply: