2011-01-07 19 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) . 

いずれも同じおよびeを解決するために私を助けることができるの過ちが繰り返されないように、また、その理由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()); 

これは、これらの両方の値がNULLの意味 "nullnull" のパスを作成しています。 上記の行の前にfd1.show();を使用してみてください。

編集:Viydhaによって指摘されたように、show()は推奨されず、setVisible()を使用する必要があります。

+0

おかげで多くを使用し、この行の前fd.show();を行う、または必要があります....それが働いていた...しかしショー()は非推奨とsestVisible() – vidhya

+0

ああ、おかげで置き換えられます。私は答えを更新します。 – Jim

1

エラーはFileDialogです。行:

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

は、nullnullのパスを作成します。あなたが良く、JFileChooser

関連する問題