Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > FindBugs complaining about non-serializable field although everythinglooks Serializable

Reply
Thread Tools

FindBugs complaining about non-serializable field although everythinglooks Serializable

 
 
laredotornado@zipmail.com
Guest
Posts: n/a
 
      01-12-2012
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
 
Reply With Quote
 
 
 
 
Jeff Higgins
Guest
Posts: n/a
 
      01-12-2012
On 01/12/2012 11:40 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
laredotornado@zipmail.com
Guest
Posts: n/a
 
      01-12-2012
On Jan 12, 11:04*am, Jeff Higgins <(E-Mail Removed)> wrote:
> On 01/12/2012 11:40 AM, (E-Mail Removed) 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
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      01-12-2012
On 1/12/12 8:40 AM, (E-Mail Removed) 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.

 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      01-12-2012
On 1/12/12 9:48 AM, (E-Mail Removed) wrote:
> On Jan 12, 11:04 am, Jeff Higgins<(E-Mail Removed)> wrote:
>> On 01/12/2012 11:40 AM, (E-Mail Removed) 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.
 
Reply With Quote
 
Ian Shef
Guest
Posts: n/a
 
      01-12-2012
"(E-Mail Removed)" <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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.
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      01-13-2012
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/wikipedi.../c/cf/Friz.jpg
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      01-15-2012
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

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      01-15-2012
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/wikipedi.../c/cf/Friz.jpg
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Object implements Serializable, but includes field(s) that do NOT implement Serializable Jimmy Java 4 08-08-2007 12:22 PM
Remove a Category from findbugs? comp.lang.java.programmer Java 2 02-23-2007 03:50 PM
How to use Findbugs 1.1 (as compared to say .95) opalpa@gmail.com opalinski from opalpaweb Java 2 10-02-2006 04:21 PM
How can I view the findbugs report in an alpha order hongjie.yang@gmail.com Java 0 05-16-2006 03:06 AM
findbugs for C/C++? yusufm@gmail.com C++ 5 03-05-2006 02:31 PM



Advertisments