2011-01-07 132 views
0

我一直在尝试做一个对象序列化,反序列化程序。当我直接在FileInputStream中给出文件名时,我的程序运行得非常好,我能够成功反序列化对象。但是当我试图使用FileDialog,序列化程序运行良好,但反序列化不能完成。我收到以下错误:对象序列化

java.io.FileNotFoundException: nullnull (The system cannot find the file specified) 
at java.io.FileInputStream.open(Native Method) 
    at java.io.FileInputStream.<init>(Unknown Source) 
    at java.io.FileInputStream.<init>(Unknown Source) 
    at package1.Deserialisation.main(Deserialisation.java:22) 
Exception in thread "main" java.lang.NullPointerException 
    at package1.Deserialisation.main(Deserialisation.java:43) . 

任何一个可以帮助我解决同样的安德也解释breif的原因吧,这样的错误不再重演..

以下是我的代码:

 package package1; 
     public class Employee implements java.io.Serializable 
     { 
    private static final long serialVersionUID = 2L; 

    String name; 
    String address; 
    int age; 
    //int SSN; 
    transient int SSN;  
    public void checkDetails() 
    { 
     System.out.println("The check details for the employee" + name + "are :"); 
    } 

} 

     Serialisation: 
     package package1; 
     import java.awt.FileDialog; 
     import java.awt.Frame; 
     import java.io.FileOutputStream; 
     import java.io.IOException; 
     import java.io.ObjectOutputStream; 

     public class Serialization 
     { 
    public static void main(String[] args) 
    { 
      Employee emp =new Employee(); 
      emp.name="vidhya"; 
      emp.address="XYZ"; 
      emp.age=26; 
      emp.SSN=123456; 
      try 
      { 
      FileDialog fd =new FileDialog(new Frame(),"Save As...",FileDialog.SAVE); 

      fd.setVisible(true); 
      String filepath=new String (fd.getDirectory()+fd.getFile()); 
      FileOutputStream fileout =new FileOutputStream(filepath); 
    ObjectOutputStream objout=new ObjectOutputStream(fileout); 
    objout.writeObject(emp); 
    fileout.close(); 
    objout.close(); 
    } 
      catch (IOException e2) { 
     e2.printStackTrace(); 
     } 
      } 
     } 


    Deserialisation : 
     package package1; 
     import java.awt.FileDialog; 
     import java.awt.Frame; 
     import java.io.FileInputStream; 
     import java.io.IOException; 
     import java.io.ObjectInputStream; 
     public class Deserialisation 
     { 
     static Employee emp=null; 
    public static void main(String[] args) 
    { 
     try { 
     FileDialog fd1 =new FileDialog(new Frame(),"Open...",FileDialog.LOAD); 
     String filepath = new String(fd1.getDirectory() + fd1.getFile()); 
     FileInputStream filein =new FileInputStream(filepath); 
     ObjectInputStream objin =new ObjectInputStream(filein); 
     emp=(Employee) objin.readObject(); 
     objin.close(); 
     filein.close(); 
     } 

     catch (IOException e) 
     { 
     e.printStackTrace(); 
     } 
     catch (ClassNotFoundException e1) 
     { 
      System.out.println("Employee class not found"); 
      e1.printStackTrace(); 
      return;  
     } 

     System.out.println("Employee Name : " + emp.name); 
     System.out.println("Address :" + emp.address); 
     System.out.println("Age : " + emp.age); 
     System.out.println("SSN :" + emp.SSN); 
    } 

} 

回答

4
String filepath = new String(fd1.getDirectory() + fd1.getFile()); 

这是创建的“nullnull”,意思是这两个值都为空的路径。 尝试在上面的行之前使用fd1.show();

编辑:正如Viydha show()指出的那样,不推荐使用setVisible()。

+0

非常感谢....它的工作...但show()已弃用,取而代之的是sestVisible() – vidhya 2011-01-07 11:45:09

1

错误在您的FileDialog。该行:

String filepath = new String (fd.getDirectory()+fd.getFile()); 

创建一条路径nullnull。你应该在这条线之前做一个fd.show();,或者更好地使用一个JFileChooser