Page 3 of 4 FirstFirst 1234 LastLast
Results 41 to 60 of 61
Like Tree1Likes

Thread: Code Help: Serialization

  1. #41
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Error opening file.
    That message is still being shown without a stack trace. Go add a printStackTrace call to the same catch block that is printing that message.
    I bet that there is an error opening the file and input is being left null. We need to see the text of the error message to be able to help.

  2. #42
    bwilson0117's Avatar
    bwilson0117 is offline Java Beginner
    Join Date
    Jul 2011
    Location
    San Antonio, TX
    Posts
    33
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    That message is still being shown without a stack trace. Go add a printStackTrace call to the same catch block that is printing that message.
    I bet that there is an error opening the file and input is being left null. We need to see the text of the error message to be able to help.
    This is the code:
    Java Code:
    try // open file
    	      {
    	         input = new ObjectInputStream(
    	            new FileInputStream( "StudentGPA.ser" ) );
    	      } // end try
    	     [COLOR="blue"] catch ( IOException ioException )
    	      {
    	    	 
    	    		System.err.println( "Error opening file." );
    	      } // end catch[/COLOR]
    	   } // end method openFile
    This blue is the catch block that is printing error opening file, how do I add the printStackTrace call to that catch block, how should it be coded.

  3. #43
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    You just did it in your previous post.

  4. #44
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Look at the printStackTrace() call you made in post#39.
    printStackTrace() is a method for a class. What class is that? Look in the API doc.
    Every catch block has the name of the class it is catching the exception for and a name for the exception variable.
    That variable is a reference to the exception object that has the information about the exception being caught.
    One of the things you want to see is the stack trace for when the exception occurred. To get that displayed, you call the printStackTrace() method for that object.

  5. #45
    bwilson0117's Avatar
    bwilson0117 is offline Java Beginner
    Join Date
    Jul 2011
    Location
    San Antonio, TX
    Posts
    33
    Rep Power
    0

    Default

    I actually figured out what was wrong, I did not have the .ser file in the same directory as my other classes. But now when I compile my code it does compile but all I am getting back is:

    Account First Name Last Name Balance
    Unable to create object.

    Which is nothing like the output that she stated we should get which is below:

    Sample toString Output
    Jerry Springer
    Student ID: 12345678
    Class Grouping: 1 and GPA = 1.3578

    Sample Output (additional processing)
    The number of students is: 26
    The average GPA is 3.2145

  6. #46
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Unable to create object.
    What does this line mean? Is it another error that needs a printStackTrace to give the FULL error message?

    While you are adding one where this message came from, be sure to add one in ALL of the catch blocks.

  7. #47
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Do a printStackTrace() in that catch block.
    But I would lay good odds on it being because your class is not the class that serialised that object data (ie created that file).

    Serialisation is tied closely to the class that made it...and your class is not your lecturers class, as I said above.

  8. #48
    bwilson0117's Avatar
    bwilson0117 is offline Java Beginner
    Join Date
    Jul 2011
    Location
    San Antonio, TX
    Posts
    33
    Rep Power
    0

    Default

    Java Code:
     catch ( EOFException endOfFileException )
    	      {
    	         return; // end of file was reached
    	      } // end catch
    	     [COLOR="blue"] catch ( ClassNotFoundException classNotFoundException )
    	      {
    	         System.err.println( "Unable to create object." );
    	      } // end catch[/COLOR]
    	      catch ( IOException ioException )
    	      {
    	         System.err.println( "Error during reading from file." );
    	      } // end catch
    	   } // end method readRecords
    It's a classnotfoundexception

  9. #49
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    And?
    Print the stack trace....
    You are losing a ton of useful error information by not doing that in your catch blocks.

  10. #50
    bwilson0117's Avatar
    bwilson0117 is offline Java Beginner
    Join Date
    Jul 2011
    Location
    San Antonio, TX
    Posts
    33
    Rep Power
    0

    Default

    Java Code:
    catch ( ClassNotFoundException classNotFoundException )
    	      {
    	    	 
    	        		[COLOR="blue"] try {
    					record = ( GpaRecordSerializable ) input.readObject();
    				
    				} catch (ClassNotFoundException e) {
    					
    					e.printStackTrace();
    				} catch (IOException e) {
    					
    					e.printStackTrace();
    				}
    	        	 {[/COLOR]
    	    		System.err.println( "Unable to create object." );
    	      } // end catch}
    I added whats in blue to my code, and I got the error:

    Account First Name Last Name Balance
    java.lang.ClassNotFoundException: StudentGPASerializable
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 47)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at java.io.ObjectInputStream.resolveClass(ObjectInput Stream.java:603)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectI nputStream.java:1574)
    at java.io.ObjectInputStream.readClassDesc(ObjectInpu tStream.java:1495)
    at java.io.ObjectInputStream.readOrdinaryObject(Objec tInputStream.java:1731)
    at java.io.ObjectInputStream.readObject0(ObjectInputS tream.java:1328)
    at java.io.ObjectInputStream.readObject(ObjectInputSt ream.java:350)
    at GpaRecord.readRecords(GpaRecord.java:39)
    at GpaRecordTest.main(GpaRecordTest.java:10)
    Unable to create object.

  11. #51
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    ClassNotFoundException: StudentGPASerializable
    Where is that class defined? Do you have a definition for it?
    Looking at your post#18 that string of text was in the .ser file you posted.

    I think it is time to get some more information from your teacher. (S)he has not given you all you need to continue with this assignment. See post#47 also.
    Last edited by Norm; 07-21-2011 at 06:46 PM.

  12. #52
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    The code you just posted is messed up. The only things that should be in the catch block are these:
    Java Code:
       catch ( ClassNotFoundException classNotFoundException )
       {
    	  classNotFoundException.printStackTrace();
    	  System.err.println( "Unable to create object." );
       } // end catch

  13. #53
    bwilson0117's Avatar
    bwilson0117 is offline Java Beginner
    Join Date
    Jul 2011
    Location
    San Antonio, TX
    Posts
    33
    Rep Power
    0

    Default

    Ok I changed my GpaRecordSerializable class to StudentGPASerializable, then I got an:
    Account First Name Last Name Balance
    Error during reading from file.

    So I added the printStackTrace method in my code like so:

    Java Code:
    catch ( IOException ioException )
    	      {
    	    	  ioException.printStackTrace();
    	         System.err.println( "Error during reading from file." );
    	      } // end catch
    	   } // end method readRecords
    Then I got this error:


    Account First Name Last Name Balance
    java.io.InvalidClassException: StudentGPASerializable; local class incompatible: stream classdesc serialVersionUID = 5929758112334863439, local class serialVersionUID = -7446631711417245029
    at java.io.ObjectStreamClass.initNonProxy(ObjectStrea mClass.java:562)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectI nputStream.java:1582)
    at java.io.ObjectInputStream.readClassDesc(ObjectInpu tStream.java:1495)
    at java.io.ObjectInputStream.readOrdinaryObject(Objec tInputStream.java:1731)
    at java.io.ObjectInputStream.readObject0(ObjectInputS tream.java:1328)
    at java.io.ObjectInputStream.readObject(ObjectInputSt ream.java:350)
    at GpaRecord.readRecords(GpaRecord.java:39)
    at GpaRecordTest.main(GpaRecordTest.java:10)
    Error during reading from file.

  14. #54
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    java.io.InvalidClassException: StudentGPASerializable; local class incompatible: stream classdesc serialVersionUID = 5929758112334863439, local class serialVersionUID = -7446631711417245029
    Your teacher has not given you enough information to do this project. Classes can be assigned Unique IDs to identify their versions. I've never used this feature.
    To give your class an ID add this statement following the class statement. I don't know what value to give it so I coded it with ???

    final static long serialVersionUID = ???;

  15. #55
    bwilson0117's Avatar
    bwilson0117 is offline Java Beginner
    Join Date
    Jul 2011
    Location
    San Antonio, TX
    Posts
    33
    Rep Power
    0

    Default

    Do you think if I just did with she said:

    4. Once the basic processing requirements have been successfully met, add code to the processing class to do the following (NOTE: You may not use an array for this processing; you may not read the file more than once):
    a. Count the number of records in the serialized file
    b. Calculate an overall average GPA for all records in the file
    c. Print the count of records and the average GPA in the format shown below

    Would I just be adding that to my StudentGPASerialized class, I think by doing that I could avoid the final static long serialVersionUID = ???; method.

  16. #56
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    I have no idea how to "count" the "records" in a .ser file. I don't know what is meant by a 'record' in a serialized file.
    My idea of what is in a serialized file is that it is a "snapshot" of the current state of a class instance. The contents of all the variables are written to the file. However how the system converts the contents of a class and writes that information to a file is hidden within the system and is not something that should be of any concern to a student.
    A snapshot of an instance of a class file is written out so that later it can be read in and the class instance can be recreated in exactly the same state it was in before it was serialized to the file.

    What the instructor means is for you to use the instance of the class(when you create it from the .ser file) to get to the data it contains. The class must have methods you can use to get the data from the instance.
    Last edited by Norm; 07-21-2011 at 07:59 PM.

  17. #57
    bwilson0117's Avatar
    bwilson0117 is offline Java Beginner
    Join Date
    Jul 2011
    Location
    San Antonio, TX
    Posts
    33
    Rep Power
    0

    Default

    Ok I think all I need to do is use the private static final long serialVersionUID = 1L;, but it is saying that the client version of StudentGPA is different from the version on the server side. So how do I make sure I have the same class version? Do you have any idea?

  18. #58
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    how do I make sure I have the same class version? Do you have any idea?
    Whoever created the .ser file MUST give you a .class file to go with it.

    From the error message:
    final static long serialVersionUID = 5929758112334863439L;

  19. #59
    bwilson0117's Avatar
    bwilson0117 is offline Java Beginner
    Join Date
    Jul 2011
    Location
    San Antonio, TX
    Posts
    33
    Rep Power
    0

    Default

    Ok I actually fixed that issue but now I am getting error:


    Account First Name Last Name Balance
    java.io.InvalidClassException: GpaRecord; GpaRecord; class invalid for deserialization
    at java.io.ObjectStreamClass.checkDeserialize(ObjectS treamClass.java:713)
    at java.io.ObjectInputStream.readOrdinaryObject(Objec tInputStream.java:1732)
    at java.io.ObjectInputStream.readObject0(ObjectInputS tream.java:1328)
    at java.io.ObjectInputStream.readObject(ObjectInputSt ream.java:350)
    at GpaRecord.readRecords(GpaRecord.java:39)
    at GpaRecordTest.main(GpaRecordTest.java:10)
    Caused by: java.io.InvalidClassException: GpaRecord; class invalid for deserialization
    at java.io.ObjectStreamClass.initNonProxy(ObjectStrea mClass.java:587)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectI nputStream.java:1582)
    at java.io.ObjectInputStream.readClassDesc(ObjectInpu tStream.java:1495)
    at java.io.ObjectInputStream.readOrdinaryObject(Objec tInputStream.java:1731)
    ... 4 more
    Error during reading from file.

  20. #60
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    java.io.InvalidClassException: GpaRecord; GpaRecord; class invalid for deserialization
    I can't tell you any more than that. There is a problem with the .ser file or ???

    Where is the class GpaRecord defined?

    Go read the API doc for the error class: InvalidClassException
    Last edited by Norm; 07-21-2011 at 08:42 PM.

Page 3 of 4 FirstFirst 1234 LastLast

Similar Threads

  1. Serialization
    By sunde887 in forum New To Java
    Replies: 4
    Last Post: 02-15-2011, 10:47 PM
  2. serialization
    By elsa14290 in forum Advanced Java
    Replies: 1
    Last Post: 02-06-2010, 04:03 PM
  3. Serialization
    By thayalan in forum Advanced Java
    Replies: 4
    Last Post: 08-03-2009, 10:22 PM
  4. Need help using serialization
    By xcallmejudasx in forum New To Java
    Replies: 0
    Last Post: 12-02-2008, 08:23 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •