Check out your constructors. They do not initialize class variables.
And that's why your classes have null values assigned to variables,
which cause NullPointerException.

On Nov 13, 2:29 pm, papatya kartal <[email protected]> wrote:
> Hi all, this is one of my last homeworks but i really stop.I'm
> frightened of not to reach the end of Java course:(
> now it is :
> Exception in thread "main" java.lang.NullPointerException
> at DeserializeMyClass.main(DeserializeMyClass.java:43)
> Java Result: 1
>
> System.out.println("School: " + mySchool.toString());
>
> What is wrong with this statement in DeserializeMyClass?
> ___________________________________________________________________________ 
> _________
> import java.io.Serializable;
>
> public class Profile implements Serializable{
>  public Profile(){
>
>     }
>     static void getProfile(String string, int i, String string0) {
>
>     }
>
> private String name;
>         private int age;
>         private String hobby;
>
>      public Profile(String name, int age,String hobby){
>          this.name = name;
>          this.age = age;
>          this.hobby=hobby;
>      }
>
>      public String getName(){
>         return name;
>     }
>
>     public void setName(String name){
>         this.name = name;
>     }
>     public int getAge(){
>         return age;
>     }
>     public void setAge(int age){
>         this.age=age;
>     }
>   public String getHobby(){
>         return hobby;
>     }
>
>     public void setHobby(String hobby){
>         this.hobby = hobby;
>     }
>     �...@override
>      public String toString(){
>          StringBuffer myBuffer = new StringBuffer();
>          myBuffer.append(this.name+" ,"+this.age+ "," +this.hobby);
>          return myBuffer.toString();
>      }
>
> }
>
> ___________________________________________________________________________ 
> ________
> import java.io.Serializable;
>
> public class School implements Serializable{
> public School(){
>
>      }
>     static void getSchool(String string, int i) {
>
>     }
>
>      private String nameOfSchool;
>      transient private int yearStarted;
>
>     public String getNameOfSchool() {
>         return nameOfSchool;
>     }
>
>     public int getYearStarted() {
>         return yearStarted;
>     }
>
>     public void setNameOfSchool(String nameOfSchool){
>
>     }
>     public void setYearStarted(int yearStarted){
>
>     }
>     @Override
>      public String toString(){
>          StringBuffer myBuffer = new StringBuffer();
>          myBuffer.append(this.nameOfSchool+" ,"+this.yearStarted);
>          return myBuffer.toString();
>      }
>
> }
>
> ___________________________________________________________________________ 
> _______
>
> import java.io.Serializable;
> import java.util.Date;
>
> public class MyClassToBePersisted implements Serializable{
>
>  static final long serialVersionUID = -3126998878902358585L;
>
>     private Date time;
>     private School school;
>     private Profile profile;
>
>     public MyClassToBePersisted(School school, Profile profile) {
>
>     }
>
>     MyClassToBePersisted() {
>
>     }
>
>     public School getSchool(){
>          return school;
>      }
>
>     public void setProfile(Profile profile) {
>
>     }
>
>     public void setSchool(School school) {
>
>     }
>
>     public Profile getProfile(){
>          return profile;
>      }
>
>     public Date getTime() {
>         return time;
>     }}
>
> ___________________________________________________________________________ 
> ____________
> import java.io.ObjectOutputStream;
> import java.io.FileOutputStream;
> import java.io.IOException;
>
> public class SerializeMyClass  {
>  public static void main(String [] args){
>
>   String filename = "MyClassToBePersisted.ser";
>          if(args.length > 0) {
>              filename = args[0];
>          }
>
>          Profile MyClassToBePersisted =new Profile();
>
>           School.getSchool("harvard", 22);
>          Profile.getProfile("mary", 11, "paint");
>
>          // Serialize the object instance and save it in
>          // a file.
>          FileOutputStream fos = null;
>          ObjectOutputStream out = null;
>          try {
>              fos = new FileOutputStream(filename);
>              out = new ObjectOutputStream(fos);
>              out.writeObject(MyClassToBePersisted);
>              out.close();
>          } catch(IOException ex) {
>              ex.printStackTrace();
>          }
>
>          System.out.println("Current time is saved into " +
> filename);
>      }}
>
> ___________________________________________________________________________ 
> _____________
> import java.io.ObjectInputStream;
> import java.io.FileInputStream;
> import java.io.IOException;
>
> public class DeserializeMyClass {
>
> public static void main(String [] args) {
>
>         String filename = "MyClassToBePersisted.ser";
>         if(args.length > 0) {
>             filename = args[0];
>         }
>
>         MyClassToBePersisted myPers = null;
>         FileInputStream fis = null;
>         ObjectInputStream in = null;
>         try {
>             fis = new FileInputStream(filename);
>             in = new ObjectInputStream(fis);
>             myPers = (MyClassToBePersisted)in.readObject();
>             in.close();
>         } catch(IOException ex) {
>             ex.printStackTrace();
>         } catch(ClassNotFoundException ex) {
>             ex.printStackTrace();
>         }
>
>         // print out restored time
>         School mySchool = myPers.getSchool();
>         Profile myProfile = myPers.getProfile();
>
>         System.out.println("School: " + mySchool.toString());
>         System.out.println("Profile: " + myProfile.toString());
>
>     }}
>
> _______________________________________________________________________
> _____________________________________________________________________

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to