1

I write Graph object into file. Graph class is as...

import java.io.Serializable;
import java.util.ArrayList;

public class Graph implements Serializable{

    public String gid = "";
    public ArrayList<Vertex> vertices = new ArrayList<Vertex>();
    public ArrayList<Edge> edges = new ArrayList<Edge>();
    public Graph()
    {

    }
    public Graph(String id, ArrayList<Vertex> ver, ArrayList<Edge> ed)
    {
        gid = id;
        vertices = ver;
        edges = ed;
    }
    public String getId()
    {
        return gid;
    }
    public void Clear()
    {
        vertices.clear();
        edges.clear();
    }
}

The Vertex and Edge class are as follow...

import java.io.Serializable;

public class Vertex implements Serializable{

    public int vertexId;
    public String vertexLabel;
    public Vertex()
    {
        vertexId = 0;
        vertexLabel = "";
    }
    public Vertex(int id, String label)
    {
        vertexId = id;
        vertexLabel = label;
    }
    public String getVertexLabel()
    {
        return vertexLabel;
    }
    public String toString()
    {
        return vertexId + "" + vertexLabel;
    }
}


import java.io.Serializable;
public class Edge implements Serializable{
    public int sid;
    public int tid;
    public Vertex src;
    public Vertex tar;
    public String bondtype;
    int bt;
    public Edge()
    {
        sid = 0;
        tid = 0;
        src = new Vertex();
        tar = new Vertex();
        bondtype = "";
        bt = 0;
    }
    public Edge(int sourceId,int targetId,Vertex source,Vertex target,int bt)
    {
        sid = sourceId;
        tid = targetId;
        src = source;
        tar = target;
        this.bt = bt;
        if(bt == 1)
            bondtype = "s";
        else if(bt == 2)
            bondtype = "d";
        else
            bondtype = "t";
    }
    public String toString()
    {
        return src.vertexLabel + "," + bondtype + "," + tar.vertexLabel;
    }
    public String reverseString()
    {
        return tar.vertexLabel + "," + bondtype + "," + src.vertexLabel;
    }
    public String toNumString()
    {
        return src.vertexId + "," + bondtype + "," + tar.vertexId;
    }
    public int getSourceId(Edge edge)
    {
        int source = 0;
        source = edge.sid;
        return source;
    }
    public int getTargetId(Edge edge)
    {
        int target = 0;
        target = edge.tid;
        return target;
    }
}

I write and read Graph object to/from file as

public static ArrayList<Graph> ReadGraphs(String filename)
    {
        ArrayList<Graph> graphs = new ArrayList<Graph>();
        ObjectInputStream ois = null;
        Graph obj = new Graph();
        try{
             ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(filename), 16 * 1024));
             while( (obj = (Graph) ois.readObject()) != null) 
            {   
                if (obj instanceof Graph) 
                {
                    graphs.add(obj);
                }
            }
         }catch (EOFException ex) {  //This exception will be caught when EOF is reached
             //System.out.println("End of file reached.");
         } catch (ClassNotFoundException ex) {
             ex.printStackTrace();
         } catch (FileNotFoundException ex) {
             ex.printStackTrace();
         } catch (IOException ex) {
             ex.printStackTrace();
         } finally {
             try {
                 if (ois != null) {
                     ois.close();
                 }
             } catch (IOException ex) {
                 ex.printStackTrace();
             }
         }
        return graphs;
    }


public static void writeG(Graph g) throws IOException
    {
        File myFile = new File("./Graphs.txt");
        int bufferSize = 16 * 1024;
        if(!myFile.exists())
        {
            myFile.createNewFile();
            FileOutputStream fos = new FileOutputStream(myFile);
            ObjectOutputStream os = new ObjectOutputStream(new BufferedOutputStream(fos,bufferSize));
            try
             {
                os.writeObject(g);
             }
             catch (IOException e)
             {
                e.printStackTrace();
             }
            os.flush();
            os.close();
          }
        else
        {
            FileOutputStream fos =  new FileOutputStream(myFile,true);
            NoHeaderObjectOutputStream oos = new NoHeaderObjectOutputStream(new BufferedOutputStream(fos, bufferSize));
            try
             {
                oos.writeObject(g);
             }
             catch (IOException e)
             {
                e.printStackTrace();
             }
          oos.flush();
          oos.close();
        }
    }

When i run it I get following class cast exception. Here is the stack trace of the problem I'm encountering:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: cannot assign instance of java.lang.String to field InputGraphPhase.Edge.src of type InputGraphPhase.Vertex in instance of InputGraphPhase.Edge
    at java.io.ObjectStreamClass$FieldReflector.setObjFieldValues(ObjectStreamClass.java:2083)
    at java.io.ObjectStreamClass.setObjFieldValues(ObjectStreamClass.java:1261)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1996)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1915)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1798)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
    at java.util.ArrayList.readObject(ArrayList.java:771)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1017)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1893)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1798)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1990)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1915)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1798)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
    at InputGraphPhase.GraphParser.ReadGraphs(GraphParser.java:501)
    at GUI.MyFrame.check(MyFrame.java:1135)
    at GUI.MyFrame.showResult(MyFrame.java:1328)
    at GUI.MyFrame$6.actionPerformed(MyFrame.java:671)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
1
  • 1
    Java serialization does not produce "text" files - and be careful to not change classes or you won't be able to read back data. Commented Nov 12, 2015 at 6:46

1 Answer 1

2
cannot assign instance of java.lang.String to field InputGraphPhase.Edge.src of type InputGraphPhase.Vertex in instance of InputGraphPhase.Edge
  1. You've change the Edge class since you serialized it. When you serialized it, src was a String; when you deserialized it it was declared as a Vertex.

  2. This loop isn't valid:

    while( (obj = (Graph) ois.readObject()) != null)
    

    readObject() doesn't return null at end of stream. It throws EOFException. A null can occur anywhere in the stream, any time you serialize one.

  3. A serialized file is not a text file. It is binary.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.