سؤال

I am trying to serialize my arraylist which holds multiple help group objects. I know I am saving the file right, but I get a ClassCastException when trying to deserialize it. The below is being called from a HelpGroup.java class that does implement serializable as well. The code below is actually found in a class called HelpGuide. Any help would be appreciated.

stack trace

SEVERE: Servlet.service() for servlet [WebAppHelp] in context with path [/webapphelpui] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: java.io.ObjectStreamClass cannot be cast to java.util.ArrayList] with root cause
java.lang.ClassCastException: java.io.ObjectStreamClass cannot be cast to java.util.ArrayList
    at com.aais.helpguides.model.HelpGuide.readList(HelpGuide.java:70)
    at com.aais.helpguides.model.HelpGroup.selectHelpGroups(HelpGroup.java:131)
    at com.aais.helpguides.controller.WebAppController.showHelpGroups(WebAppController.java:43)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:662)

HelpGuide.java

package com.aais.helpguides.model;

import com.aais.helpguides.model.HelpGroup;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

public class HelpGuide 
{
    private ArrayList<HelpGroup> helpGroups;

    // default constructor
    public HelpGuide() 
    {
        this.helpGroups = new ArrayList<HelpGroup>();
    }


    public HelpGuide(ArrayList<HelpGroup> helpGroups)
    {
        this.helpGroups = new ArrayList<HelpGroup>(helpGroups);

    }
    public ArrayList<HelpGroup> getHelpGroups()
    {
        return helpGroups;
    }

    public void setHelpGroups(ArrayList<HelpGroup> helpGroups)
    {
        this.helpGroups = new ArrayList<HelpGroup>(helpGroups);
    }

    // save the current array list of help groups
        public void saveList(ArrayList<HelpGroup> helpGroups)
        {

            try
            {
                FileOutputStream fileOut =  new FileOutputStream("C:/temp/listhelpgroups.ser");
                ObjectOutputStream out = new ObjectOutputStream(fileOut);
                out.writeObject(helpGroups);
                out.close();
                fileOut.close();
                System.out.printf("Serialized data is saved in C:/temp/listhelpgroups.ser");
            }   
            catch(IOException i)
            {
                i.printStackTrace();
            }
        }

        // read the list 
        public ArrayList<HelpGroup> readList()
        {
            ArrayList<HelpGroup> currentList = new ArrayList<HelpGroup>();
            try
            {
                FileInputStream fis = new FileInputStream("C:/temp/listhelpgroups.ser");
                ObjectInputStream ois = new ObjectInputStream(fis);
                while(fis.read() != -1)
                {
                    currentList = (ArrayList<HelpGroup>) ois.readObject();
                }
            } 
            catch (ClassNotFoundException e) 
            {
                e.printStackTrace();
            }
            catch (FileNotFoundException e) 
            {
                e.printStackTrace();
            }
            catch (IOException e) 
            {
                e.printStackTrace();
            }
            return currentList;
        }

class to be serialized

public class HelpGroup implements Serializable{
    @NotNull
    private long id;
    @NotNull
    private int sequence;
    @NotEmpty
    private String name;

    private String description;

    private ArrayList<HelpSection> helpSections;

    // default constructor
    public HelpGroup() {
        this.helpSections = new ArrayList<HelpSection>();
    }

    // constructor with specific arguments
    public HelpGroup(String name, long id, String description, int sequence) {
        this.description = description;
        this.id = id;
        this.name = name;
        this.sequence = sequence;
    }

    // get id
    public long getId() {
        return id;
    }

    // set id
    public void setId(long id) {
        this.id = id;
    }

    // get name
    public String getName() {
        return name;
    }

    // set name
    public void setName(String name) {
        this.name = name;
    }

    // get description
    public String getDescription() {
        return description;
    }

    // set description
    public void setDescription(String description) {
        this.description = description;
    }

    // get sequence; order in which they are listed
    public int getSequence() {
        return sequence;
    }

    // set the sequence
    public void setSequence(int sequence) {
        this.sequence = sequence;
    }
هل كانت مفيدة؟

المحلول

You are doing the read part wrong

while(fis.read() != -1)
{
    currentList = (ArrayList<HelpGroup>) ois.readObject();
}

the fis.read is also reading from the stream, so that when you do ois.readObject() you are not getting a complete object.

You should be able to simply do

currentList = (ArrayList<HelpGroup>) ois.readObject();

not in a loop.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top