Page 2 of 4 FirstFirst 1234 LastLast
Results 21 to 40 of 61
Like Tree1Likes

Thread: Code Help: Serialization

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

    Default

    Look in your code for a reference to a filename that ends with .ser.
    That is one place where you can set the name of the file to be read.

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

    Default

    Java Code:
    try // open file
    	      {
    	         input = new ObjectInputStream(
    	            [COLOR="blue"]new FileInputStream( "StudentGPA.ser" ) );[/COLOR]
    	      } // end try
    	      catch ( IOException ioException )
    	      {
    	         System.err.println( "Error opening file." );
    	      } // end catch
    	   } // end method openFile
    Ok I set it here would that cause it to be read?
    I compiled but still getting blank output.

  3. #23
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    still getting blank output.
    How many printlns did you add to your code? The one in the main() method should print for sure.
    If it doesn't then you are not executing your program and must be getting an error message from the java command about not finding your program or the main method.

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

    Default

    This here is my test code:

    Java Code:
    public class GpaRecordTest
    {
       public static void main( String args[] )
       {
          GpaRecord application = new GpaRecord();
    
          application.openFile();
          application.readRecords();
          application.closeFile();
       } // end main
    } // end class GpaRecordTest
    The error/output message I am getting is:

    Account First Name Last Name Balance
    Error opening file.
    Exception in thread "main" java.lang.NullPointerException
    at GpaRecord.readRecords(GpaRecord.java:37)
    at GpaRecordTest.main(GpaRecordTest.java:10)

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

    Default

    Exception in thread "main" java.lang.NullPointerException
    at GpaRecord.readRecords(GpaRecord.java:37)
    Look at line 37 in the GpaRecord class and see what variable is null. Then back track in the code to see why that variable does not have a valid value.

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

    Default

    This is my line 37:
    Java Code:
    record = ( GpaRecordSerializable ) input.readObject();
    But I see nothing wrong with it.

  7. #27
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    see what variable is null. Then back track in the code to see why that variable does not have a valid value.

    The variable on that line is: input.
    Does it have a valid value? Add a println just before that statement to show its value.

    Your earlier post had this message in it:
    Error opening file.

    Does that mean anything to you? Why was it printed?
    Add a printStackTrace() call to the catch block to show the full text of the error you are getting.
    Look that method up in the API doc to see how to use it, or do a Search here on the Forum for samples.
    Last edited by Norm; 07-20-2011 at 11:21 PM.

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

    Default

    Im totally lost I added a println, but I am still getting the error message:

    Error opening file.
    Exception in thread "main" java.lang.NullPointerException
    at GpaRecord.readRecords(GpaRecord.java:37)
    at GpaRecordTest.main(GpaRecordTest.java:10)
    Account First Name Last Name Balance

  9. #29
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Go back and add the call to printStackTrace in the catch block where the error message was printed.
    The open of the file is failing. There is nothing to read after that fails. You must print out the full text of the error message to see what the error is so that you can fix it. The silly message that is printed out is hiding valuable information about the problem.

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

    Default

    How do I print out the whole text of the error message?

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

    Default

    add the call to printStackTrace in the catch block where the error message was printed.

    I think I've asked already in post#27 & post #29

    Use google or Search to get more information if you need it: Java printstacktrace
    Last edited by Norm; 07-21-2011 at 03:53 AM.

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

    Default

    I understand exactly what you are saying, what I am saying is that I am not familiar with the printStackTrace method or concept, we haven't covered that subject in my class yet.

  13. #33
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Without it you will not know what the error is and then be able to fix it.

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

    Default

    Where should I go to read up on it?

  15. #35
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Do you know of Google?
    Ask Google: java printStackTrace

  16. #36
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,049
    Rep Power
    20

    Default

    Someone'll correct me if I'm wrong, but isn't the idea of having serialised data that was serialised in one version of a class, and then asking someone to write a class with the same sort of data in it in order to read this serialised data simply not going to work?
    What are the odds the serialisation is going to be exactly the same for the two (essentially different) classes?

    This strikes me as a very bad exercise.
    Norm likes this.

  17. #37
    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 Tolls View Post
    Someone'll correct me if I'm wrong, but isn't the idea of having serialised data that was serialised in one version of a class, and then asking someone to write a class with the same sort of data in it in order to read this serialised data simply not going to work?
    What are the odds the serialisation is going to be exactly the same for the two (essentially different) classes?

    This strikes me as a very bad exercise.
    I believe this is my Professor's first time actually teaching this class, so she's just throwing anything at us.

  18. #38
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,049
    Rep Power
    20

    Default

    Oh joy.

    For the printStackTrace() you'll need something like:
    Java Code:
    try {
       .. some code ..
    } catch (SomeException ex) {
        ex.printStackTrace();
    }
    This will print the stack trace for that exception to System.out.
    That will give you line numbers for where the exception was thrown.

  19. #39
    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 Tolls View Post
    Oh joy.

    For the printStackTrace() you'll need something like:
    Java Code:
    try {
       .. some code ..
    } catch (SomeException ex) {
        ex.printStackTrace();
    }
    This will print the stack trace for that exception to System.out.
    That will give you line numbers for where the exception was thrown.
    I am not sure if I did this right but this is my code:
    Java Code:
     try {
    	        		 [COLOR="blue"]record = ( GpaRecordSerializable ) input.readObject();[/COLOR]
    	        		} catch (Exception ex) {
    	        		    ex.printStackTrace();
    	        		}
    	        	[COLOR="blue"] record = ( GpaRecordSerializable ) input.readObject();[/COLOR]
    My error message was

    Error opening file.
    Account First Name Last Name Balance
    java.lang.NullPointerException
    at GpaRecord.readRecords(GpaRecord.java:39)
    at GpaRecordTest.main(GpaRecordTest.java:10)
    Exception in thread "main" java.lang.NullPointerException
    at GpaRecord.readRecords(GpaRecord.java:43)
    at GpaRecordTest.main(GpaRecordTest.java:10)

    lines 39, and 43 are highlighted in blue in my above code.
    It's like that line is throwing everything off.

  20. #40
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,049
    Rep Power
    20

    Default

    That's right.
    So something on line 39 of GpaRecord.java is null.

    Just to say, though, that it's nice to see someone using the proper naming conventions. It makes our life, reading your code, so much easier.

Page 2 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
  •