2011-01-07 3 views
0

나는 객체 직렬화, deserialisation 프로그램을하려고 노력했다. FileInputStream에서 직접 File Name을 주었을 때, 프로그램이 아주 정교하게 실행되었고, 객체를 성공적으로 deserialize 할 수 있었다. 그러나 나는 시도했다. FileDialog를 사용하면 serialization 프로그램이 잘 실행되지만 deserialisation은 수행 할 수 없습니다. 나는 다음과 같은 오류가 점점 오전 : 다음객체 직렬화

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) . 

어느 한 실수가 반복되지 않도록 같은 ANDE이 또한 이유 대한 간략한에서 설명 해결하는 데 도움이 수 ...

은 내 코드입니다 :

 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

고마워 ... ... show()는 더 이상 사용되지 않으며 sestVisible()로 대체됩니다. – vidhya

+0

감사합니다. 나는 대답을 업데이트 할 것이다. – Jim

1

오류는 FileDialog에 있습니다. 줄 :

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

nullnull의 경로를 만듭니다. 이 줄 앞에 fd.show();을 실행하거나 더 좋게 사용하십시오. JFileChooser