从ObjectStreamClass到util.ArrayList的类Cast Exception

时间:2014-04-30 00:10:52

标签: java serialization arraylist

我正在尝试序列化包含多个帮助组对象的arraylist。我知道我正在保存文件,但是在尝试反序列化时我得到了ClassCastException。以下是从HelpGroup.java类调用的,它实现了可序列化。下面的代码实际上位于一个名为HelpGuide的类中。任何帮助将不胜感激。

堆栈跟踪

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;
        }

要序列化的类

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;
    }

1 个答案:

答案 0 :(得分:0)

您正在读取错误的部分

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

fis.read也是从流中读取的,因此当你执行ois.readObject()时,你没有得到完整的对象。

你应该可以简单地做

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

不在循环中。