java.io.NotSerializableException:

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madhoriya22
    Contributor
    • Jul 2007
    • 251

    java.io.NotSerializableException:

    Hi,
    I am getting this exception while deploying my web application. It is occuring for the attributes which I have set for the session. Here is the method which sets the session attribues :----
    Code:
    protected void showIndexPage(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException{
            
            PrintWriter out = response.getWriter();
            HttpSession session = request.getSession(true);
            response.setContentType("text/html;charset=UTF-8");
            System.out.println("Inside showIndexPage() method");
            
            DAOFactory mysqlFactory = DAOFactory.getDAOFactory(DAOFactory.MYSQL);
            System.out.println("below DAOFactory object------------->>");
            OffshoreResourceDAO offshoreResourceDAO = mysqlFactory.getOffshoreResourceDAO();
            Vector offshoreResourceVOList = offshoreResourceDAO.getResource();
            
            ClientResourceDAO clientResourceDAO = mysqlFactory.getClientResourceDAO();
            Vector clientResourceVOList = clientResourceDAO.getClientResource();
            
            WorkPackageDetailsDAO workPackageDetailsDAO = mysqlFactory.getWorkPackageDetailsDAO();
            Vector workPackageDetailsList = workPackageDetailsDAO.getWorkPackageDetails();
            System.out.println("WorkPackageDetails are:"+workPackageDetailsList);
            
            Vector weekList = weekFinder();
            System.out.println("Week List is:"+weekList);
            
            session.setAttribute("OffshoreResourceList",offshoreResourceVOList);
            session.setAttribute("ClientResourceList", clientResourceVOList);
            session.setAttribute("WorkPackageDetailsList", workPackageDetailsList);
            session.setAttribute("WeekList", weekList);
            request.getRequestDispatcher("index.jsp").forward(request,response);
            
            out.close();
        }
    The attributes for which I am getting this exception are OffshoreResourc eList,ClientRes ourceList,WorkP ackageDetailsLi st. All these are Vectors.While throwing exception, this warning is comin:---
    Code:
    WARNING: Cannot serialize session attribute OffshoreResourceList for session 5F8A4B888C40A6D0C72FB2B67A60A641
    How I can remove this exception.
    Thanks and regards,
    madhoriya
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Make your OffshoreResourc eList et al implement the Serializable interface. This
    is extremely easy because that interface doesn't define any methods, so simply
    do this:

    [code=java]
    public class OffshoreResourc eList implements Serializable {
    ...
    }
    [/code]

    Do the same for all the other classes that can't be serialized now.

    kind regards,

    Jos

    Comment

    • madhoriya22
      Contributor
      • Jul 2007
      • 251

      #3
      Originally posted by JosAH
      Make your OffshoreResourc eList et al implement the Serializable interface. This
      is extremely easy because that interface doesn't define any methods, so simply
      do this:

      [code=java]
      public class OffshoreResourc eList implements Serializable {
      ...
      }
      [/code]

      Do the same for all the other classes that can't be serialized now.

      kind regards,

      Jos
      Hi Jos,
      It's Ok. But I have read the Vector class documentation.. .It already implements the serializabe interface. Then why we have to serialize it again in this class

      Thanks and regards,
      madhoriya

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by madhoriya22
        Hi Jos,
        It's Ok. But I have read the Vector class documentation.. .It already implements the serializabe interface. Then why we have to serialize it again in this class

        Thanks and regards,
        madhoriya
        Ah, ok, then the error diagnostic indicates that one of the members you've put
        in that Vector doesn't implement the Serializable interface. I didn't know that
        your class was a Vector itself.

        kind regards,

        Jos

        Comment

        • madhoriya22
          Contributor
          • Jul 2007
          • 251

          #5
          Originally posted by JosAH
          Ah, ok, then the error diagnostic indicates that one of the members you've put
          in that Vector doesn't implement the Serializable interface. I didn't know that
          your class was a Vector itself.

          kind regards,

          Jos
          Hi Jos,
          Then what i have to do to remove this exception. See what I am putting in this vector is object of a Value Object class.... which is made of getters and setters methods.
          These objects contains the values fetched from the database means every object has his ints, strings, double etc....

          Thanks and regards,
          madhoriya

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by madhoriya22
            Hi Jos,
            Then what i have to do to remove this exception. See what I am putting in this vector is object of a Value Object class.... which is made of getters and setters methods.
            These objects contains the values fetched from the database means every object has his ints, strings, double etc....

            Thanks and regards,
            madhoriya
            It's simple:

            [code=java]
            public class ValueObject implements Serializable {
            ...
            }
            [/code]

            The ObjectInputStre am and ObjectOutputStr eam explicitly check whether or not
            a to be serialized object implements that interface; if not: *boink* they throw
            that Exception.

            So basically, an object (or a class) is Serializable if it implements the Serializable
            interface itself and all of its members are Serializable too; primitives are always
            Serializable.

            kind regards,

            Jos

            Comment

            • madhoriya22
              Contributor
              • Jul 2007
              • 251

              #7
              Originally posted by JosAH
              It's simple:

              [code=java]
              public class ValueObject implements Serializable {
              ...
              }
              [/code]

              The ObjectInputStre am and ObjectOutputStr eam explicitly check whether or not
              a to be serialized object implements that interface; if not: *boink* they throw
              that Exception.

              So basically, an object (or a class) is Serializable if it implements the Serializable
              interface itself and all of its members are Serializable too; primitives are always
              Serializable.

              kind regards,

              Jos
              Hi Jos,
              Got it !! Thanks ...

              regards,
              madhoriya

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by madhoriya22
                Hi Jos,
                Got it !! Thanks ...

                regards,
                madhoriya
                You're welcome of course; does it work now?

                kind regards,

                Jos

                ps. You could write a little article about it of course; put it in the Editor's Corner
                where I take care of the typos so we can put it in the Java Articles section
                afterwards. Just a concise article about Serialization not a (verbatim) copy of
                the text in the API documentation ...<hint hint/>

                Comment

                • madhoriya22
                  Contributor
                  • Jul 2007
                  • 251

                  #9
                  Originally posted by JosAH
                  You're welcome of course; does it work now?

                  kind regards,

                  Jos

                  ps. You could write a little article about it of course; put it in the Editor's Corner
                  where I take care of the typos so we can put it in the Java Articles section
                  afterwards. Just a concise article about Serialization not a (verbatim) copy of
                  the text in the API documentation ...<hint hint/>
                  Hi Jos,
                  Yep it worked. I vil think about your suggestion. Problem is work load.

                  Thanks and regards,
                  madhoriya

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by madhoriya22
                    Hi Jos,
                    Yep it worked. I vil think about your suggestion. Problem is work load.

                    Thanks and regards,
                    madhoriya
                    Take it easy; there's no pressure or time limit. Writing a good article under
                    pressure had never succeeded before.

                    kind regards,

                    Jos

                    Comment

                    • madhoriya22
                      Contributor
                      • Jul 2007
                      • 251

                      #11
                      Originally posted by JosAH
                      Take it easy; there's no pressure or time limit. Writing a good article under
                      pressure had never succeeded before.

                      kind regards,

                      Jos
                      Hi Jos,
                      Again Thanks for ur suggestion.
                      I vil surely do it when i vill be free minded. I really want to do it

                      thanks and regards,
                      madhoriya

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by madhoriya22
                        Hi Jos,
                        Again Thanks for ur suggestion.
                        I vil surely do it when i vill be free minded. I really want to do it

                        thanks and regards,
                        madhoriya
                        Thanks for your good intentions; I appreciate it a lot; and realize: there's no
                        hurry and we both can take care of any hurdles if that are any. It can be a nice
                        article.

                        kind regards,

                        Jos

                        Comment

                        Working...