Getting a class cast exception while using ObjectInputStream.

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 534 Views - Last Post: 06 June 2014 - 08:27 PM Rate Topic: -----

#1 jacobTheMechanic  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 89
  • Joined: 18-September 12

Getting a class cast exception while using ObjectInputStream.

Posted 05 June 2014 - 05:37 PM

Hello all,

I'm developing an application to track the status of a production flow-line and have hit a bit of a snag. When attempting to read saved data I run into this:
Exception in thread "main" java.lang.ClassCastException: flowline.End_Of_File cannot be cast to flowline.Operation
	at flowline.Station.checkLoadPreviousStationStatus(Station.java:91)
	at flowline.Station.main(Station.java:212)
Java Result: 1



I've been reading up on different methods to saving and retrieving data and have decided ObjectInputStream would be the best option.

The save method works fine, I opted to use a EndOfFile class to determine when I've reached the end of the input stream. The problem is, when my loop encounters this object, it doesn't terminate the loop.

 public void checkLoadPreviousStationStatus() throws FileNotFoundException, IOException,
                                                     ClassNotFoundException, EOFException, TempArrayOutOfBoundsException{        
            Object loadOpn = null;
            End_Of_File eof = new End_Of_File();
            File f = new File(fileName);
            //if station file already exists load the info
                if(f.exists() && !f.isDirectory()){
                    FileInputStream fis = new FileInputStream(f);
                    ObjectInputStream ois = new ObjectInputStream(fis);
                        while((loadOpn = ois.readObject()) != eof){
                            //loadOpn = ois.readObject();
                            [color="#FF0000"]opList.add((Operation)ois.readObject());[/color]
                        }
                    ois.close();
                    sortStationOperations();
                }else{
                    getOperationsFromDatabase();
                }
        }



The Operation cast is a cast to the objects my LinkedList contains. The highlighted line is where the exception occurs.

Any help is appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Getting a class cast exception while using ObjectInputStream.

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Getting a class cast exception while using ObjectInputStream.

Posted 06 June 2014 - 01:17 AM

Looking at your code:
while((loadOpn = ois.readObject()) != eof){
   opList.add((Operation)ois.readObject());

You are reading two objects in each iteration, you already read the object and assign it to loadOpn at: loadOpn = ois.readObject(), at the second line you should use that object to add it to the list and not read another object. Also you can use instance of to see if the object is an instance of a specific class before casting it.
Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,204
  • Joined: 20-September 08

Re: Getting a class cast exception while using ObjectInputStream.

Posted 06 June 2014 - 01:30 AM

Also, you'll find it a lot easier to serialize a Collection, though if your application needs to be scalable, you should use a DB
Was This Post Helpful? 1
  • +
  • -

#4 jacobTheMechanic  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 89
  • Joined: 18-September 12

Re: Getting a class cast exception while using ObjectInputStream.

Posted 06 June 2014 - 11:06 AM

So...something like this? (I'm away from my computer to test)
	while(!ois.readObject()) instanceOf eof){
	   opList.add((Operation)ois.readObject());
        }



View Postg00se, on 06 June 2014 - 01:30 AM, said:

Also, you'll find it a lot easier to serialize a Collection, though if your application needs to be scalable, you should use a DB

I plan on using a DB, right now I'm trying to get the logic of my app down. I'm using an excel file as a temp. DB now.
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Getting a class cast exception while using ObjectInputStream.

Posted 06 June 2014 - 12:35 PM

View PostjacobTheMechanic, on 06 June 2014 - 11:51 PM, said:

So...something like this? (I'm away from my computer to test)

you are stil reading two objects in each iteration,you will still need the loadOpn to store the read object so you can add it to the next line.
Also check how to use instance of, a class follows after it, not an object.
Was This Post Helpful? 0
  • +
  • -

#6 jacobTheMechanic  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 89
  • Joined: 18-September 12

Re: Getting a class cast exception while using ObjectInputStream.

Posted 06 June 2014 - 03:08 PM

I can't get it to work for me...
                        while((loadOpn = ois.readObject()) != eof){
                            if(!loadOpn.equals(eof)){
                                opList.add((Operation) loadOpn);
                            }
                            
                        }


I'll try messing around with it and maybe explore other options. I tried the instanceof option to no avail.

Thank you for your help.
Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Getting a class cast exception while using ObjectInputStream.

Posted 06 June 2014 - 03:17 PM

You didnt really do what I said, from what i see. something like this I have a good feeling will work:
while(!(loadOpn = ois.readObject()) instanceOf End_Of_File){
   opList.add((Operation)loadOpn);
       }




by the way, did you make the class End_Of_File? Really need to take care of naming convention.

This post has been edited by smohd: 06 June 2014 - 03:38 PM
Reason for edit:: added missing !

Was This Post Helpful? 1
  • +
  • -

#8 jacobTheMechanic  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 89
  • Joined: 18-September 12

Re: Getting a class cast exception while using ObjectInputStream.

Posted 06 June 2014 - 03:28 PM

View Postsmohd, on 06 June 2014 - 03:17 PM, said:

You didnt really do what I said, from what i see. something like this I have a good feeling will work:
while((loadOpn = ois.readObject()) instanceOf End_Of_File){
   opList.add((Operation)loadOpn);
       }




by the way, did you make the class End_Of_File? Really need to take care of naming convention.

I tried that, or should I say, thought I had and couldn't get it to work.

I did this:
                         EndOfFile eof = new EndOfFile();
                         while((loadOpn = ois.readObject()) instanceof eof){
                      



Should be this:


                       while((loadOpn = ois.readObject()) instanceof EndOfFile){
                         



I've used instanceof in the past but wasn't understanding what you were trying to tell me; instanceofchecks for the instance of a class...not the object of the class I was testing it against.

Thank you for your patience!

View Postsmohd, on 06 June 2014 - 03:17 PM, said:

by the way, did you make the class End_Of_File? Really need to take care of naming convention.



Yeah, I changed this yesterday after reading through my original post. private static final String END_OF_FILE
Was This Post Helpful? 0
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Getting a class cast exception while using ObjectInputStream.

Posted 06 June 2014 - 03:37 PM

View PostjacobTheMechanic, on 07 June 2014 - 04:13 AM, said:

Yeah, I changed this yesterday after reading through my original post. private static final String END_OF_FILE

Aha? not following here. So you dont have that class? Could you please post the updated code of the original piece of code? Looks like we will need to check your scenario again...
Was This Post Helpful? 0
  • +
  • -

#10 jacobTheMechanic  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 89
  • Joined: 18-September 12

Re: Getting a class cast exception while using ObjectInputStream.

Posted 06 June 2014 - 03:47 PM

No, I changed the name of the class to EndOfFile from End_Of_File. I was illustrating the proper naming convention use of the latter.

Here is my updated method:
public void checkLoadPreviousStationStatus() throws FileNotFoundException, IOException,
                                                     ClassNotFoundException, EOFException, TempArrayOutOfBoundsException{        
            Object loadOpn = null;
            File f = new File(fileName);
            //if station file already exists load the info
                if(f.exists() && !f.isDirectory()){
                    FileInputStream fis = new FileInputStream(f);
                    ObjectInputStream ois = new ObjectInputStream(fis);
                        while(!((loadOpn = ois.readObject()) instanceof EndOfFile)){
                                opList.add((Operation) loadOpn);
                        }
                    ois.close();
                    sortStationOperations();
                }else{
                    getOperationsFromDatabase();
                }
        }


Was This Post Helpful? 0
  • +
  • -

#11 jacobTheMechanic  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 89
  • Joined: 18-September 12

Re: Getting a class cast exception while using ObjectInputStream.

Posted 06 June 2014 - 03:53 PM

The method works now, in a way. The while loop is ending when it's supposed to (when it encounters an instance of EndOfFile), however, now my linked list of objects contains two of each object.
Was This Post Helpful? 0
  • +
  • -

#12 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Getting a class cast exception while using ObjectInputStream.

Posted 06 June 2014 - 04:09 PM

Are you sure it that was not caused by the serializing part?
Also as said before, serialize as collection is much easier
Was This Post Helpful? 1
  • +
  • -

#13 jacobTheMechanic  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 89
  • Joined: 18-September 12

Re: Getting a class cast exception while using ObjectInputStream.

Posted 06 June 2014 - 04:20 PM

View Postsmohd, on 06 June 2014 - 04:09 PM, said:

Are you sure it that was not caused by the serializing part?
Also as said before, serialize as collection is much easier

I'm not familiar with serializing as a collection. This represents the extent of my knowledge of serialization. I'll research the topic and try to come up with something better.

This "application" is the first outside of a classroom programming I've ever done. I'm trying to learn as I go. My reasoning in deciding to store the program data with a serialized object was:

1) My program tracks the serial number, build status (i.e. build operation completed), and build time remaining for each individual engine.
2) Each "Station" object stores all this information.
3) I could simply store each "Station" object and recall the status of the object when the user enters the particular station number on the javafx form. Otherwise, a new "Station" object is created.

I'm sure this is not the best route.
Was This Post Helpful? 0
  • +
  • -

#14 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,204
  • Joined: 20-September 08

Re: Getting a class cast exception while using ObjectInputStream.

Posted 06 June 2014 - 04:44 PM

Quote

I'm not familiar with serializing as a collection.
It's as simple as adding your objects to an ArrayList and serializing that
Was This Post Helpful? 1
  • +
  • -

#15 jacobTheMechanic  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 89
  • Joined: 18-September 12

Re: Getting a class cast exception while using ObjectInputStream.

Posted 06 June 2014 - 04:48 PM

Got it. That does sound easier. A lot easier.

Thank you for your help, I love this place!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2