为什么我把 sd=(Student)ois.readObject(); 这段改成
Student sd2=(Student)ois.readObject();
然后下面打印的地方改成 sd2.age 就会报错。import java.io.*;
class Serialization 
{
public static void main(String[] args) throws IOException,ClassNotFoundException
{
Student sd=new Student(19,"dintdding",20,"computer");
FileOutputStream fos=new FileOutputStream("Myfile.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
try
{
oos.writeObject(sd);
oos.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
sd=null;
FileInputStream fis=new FileInputStream("Myfile.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
try
{
sd=(Student)ois.readObject();
fis.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
System.out.println("id = "+sd.id);
System.out.println("name = "+sd.name);
System.out.println("age = "+sd.age);
System.out.println("department = "+sd.department);
}
}class Student implements Serializable
{
int id;
String name;
int age;
String department;
public Student(int id,String name,int age,String department)
{
this.id=id;
this.name=name;
this.age=age;
this.department=department;
}
public Student()
{
}
}

解决方案 »

  1.   

    因为你sd2是在try中定义的,如果出现异常,下面不知道sd2是什么东东。所以,如果用sd2,应该在try前面定义Student sd2
      

  2.   

    你有一行把sd=null了,当然会报空指针异常啦!
      

  3.   

    package test;import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;public class Test3
    {
            public static void main(String[] args) throws IOException,ClassNotFoundException
            {
                Student sd=new Student(19,"dintdding",20,"computer");
                Student sd2 = null;
                FileOutputStream fos=new FileOutputStream("Myfile.txt");
                ObjectOutputStream oos=new ObjectOutputStream(fos);
                try
                {
                    oos.writeObject(sd);
                    oos.close();
                }
                catch(Exception e)
                {
                    System.out.println(e.toString());    
                }
                sd=null;            FileInputStream fis=new FileInputStream("Myfile.txt");
                ObjectInputStream ois=new ObjectInputStream(fis);
                try
                {
                    sd2=(Student)ois.readObject();
                    fis.close();
                }
                catch(Exception e)
                {
                    System.out.println(e.toString());
                }
                System.out.println("id = "+sd2.id);
                System.out.println("name = "+sd2.name);
                System.out.println("age = "+sd2.age);
                System.out.println("department = "+sd2.department);
            }
        }    class Student implements Serializable
        {
            int id;
            String name;
            int age;
            String department;
            public Student(int id,String name,int age,String department)
            {
                this.id=id;
                this.name=name;
                this.age=age;
                this.department=department;    
            }
            public Student()
            {
            }
        }
      

  4.   

    多谢LS各位,确实是局部变量的问题。新手,前面没学好。
    没意识到try代码块里的变量的作用域。
    顺便问下,是不是一对大括号内定义的变量,作用域都在这对大括号内?
      

  5.   

    嗯,LZ程序是try catch问题,不是IO问题。