Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #11285

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

Newsgroups comp.lang.java.programmer
Subject Re: FindBugs complaining about non-serializable field although everything looks Serializable
From Ian Shef <invalid@avoiding.spam>
References <a3ed0747-5eb5-4da1-9f7f-c6ebdc8cbafd@v13g2000yqc.googlegroups.com>
Message-ID <Xns9FD87F16AD841vaj4088ianshef@138.125.254.103> (permalink)
Date 2012-01-12 19:29 +0000
Organization Raytheon Company

Show all headers | View raw


"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.

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

FindBugs complaining about non-serializable field although everything looks Serializable "laredotornado@zipmail.com" <laredotornado@gmail.com> - 2012-01-12 08:40 -0800
  Re: FindBugs complaining about non-serializable field although everything looks Serializable Jeff Higgins <jeff@invalid.invalid> - 2012-01-12 12:04 -0500
    Re: FindBugs complaining about non-serializable field although everything looks Serializable "laredotornado@zipmail.com" <laredotornado@gmail.com> - 2012-01-12 09:48 -0800
      Re: FindBugs complaining about non-serializable field although everything looks Serializable Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-01-12 09:52 -0800
  Re: FindBugs complaining about non-serializable field although everything looks Serializable Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-01-12 09:52 -0800
  Re: FindBugs complaining about non-serializable field although everything looks Serializable Ian Shef <invalid@avoiding.spam> - 2012-01-12 19:29 +0000
    Re: FindBugs complaining about non-serializable field although everything looks Serializable Lew <noone@lewscanon.com> - 2012-01-12 21:10 -0800
      Re: FindBugs complaining about non-serializable field although everything looks Serializable Lew <noone@lewscanon.com> - 2012-01-12 21:11 -0800
      Re: FindBugs complaining about non-serializable field although everything looks Serializable Arne Vajhøj <arne@vajhoej.dk> - 2012-01-14 23:23 -0500
        Re: FindBugs complaining about non-serializable field although everything looks Serializable Lew <noone@lewscanon.com> - 2012-01-15 10:18 -0800

csiph-web