FindBugs complaining about non-serializable field although everythinglooks Serializable

Discussion in 'Java' started by laredotornado@zipmail.com, Jan 12, 2012.

  1. Guest

    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
     
    , Jan 12, 2012
    #1
    1. Advertising

  2. Jeff Higgins Guest

    On 01/12/2012 11:40 AM, 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
     
    Jeff Higgins, Jan 12, 2012
    #2
    1. Advertising

  3. Guest

    Re: FindBugs complaining about non-serializable field althougheverything looks Serializable

    On Jan 12, 11:04 am, Jeff Higgins <> wrote:
    > On 01/12/2012 11:40 AM, 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
     
    , Jan 12, 2012
    #3
  4. Daniel Pitts Guest

    On 1/12/12 8:40 AM, 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, Jan 12, 2012
    #4
  5. Daniel Pitts Guest

    On 1/12/12 9:48 AM, wrote:
    > On Jan 12, 11:04 am, Jeff Higgins<> wrote:
    >> On 01/12/2012 11:40 AM, 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.
     
    Daniel Pitts, Jan 12, 2012
    #5
  6. Ian Shef Guest

    Re: FindBugs complaining about non-serializable field although everything looks Serializable

    "" <> wrote in
    news::

    > 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.
     
    Ian Shef, Jan 12, 2012
    #6
  7. Lew Guest

    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
     
    Lew, Jan 13, 2012
    #7
  8. 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
     
    Arne Vajhøj, Jan 15, 2012
    #8
  9. Lew Guest

    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
     
    Lew, Jan 15, 2012
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    365
  2.  opalinski from opalpaweb

    How to use Findbugs 1.1 (as compared to say .95)

    opalinski from opalpaweb, Oct 2, 2006, in forum: Java
    Replies:
    2
    Views:
    409
    opalinski from opalpaweb
    Oct 2, 2006
  3. comp.lang.java.programmer

    Remove a Category from findbugs?

    comp.lang.java.programmer, Feb 22, 2007, in forum: Java
    Replies:
    2
    Views:
    409
    comp.lang.java.programmer
    Feb 23, 2007
  4. findbugs for C/C++?

    , Mar 3, 2006, in forum: C++
    Replies:
    5
    Views:
    2,447
    Dietmar Kuehl
    Mar 5, 2006
  5. Jimmy
    Replies:
    4
    Views:
    662
    Thomas Hawtin
    Aug 8, 2007
Loading...

Share This Page