{"id":1889,"date":"2022-08-30T15:20:17","date_gmt":"2022-08-30T15:20:17","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/object-serialisation-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:20:17","modified_gmt":"2022-08-30T15:20:17","slug":"object-serialisation-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/object-serialisation-collection-of-common-programming-errors\/","title":{"rendered":"Object Serialisation-Collection of common programming errors"},"content":{"rendered":"<p>I had been trying to do an object serialiation,deserialisation program.When i am giving the File Name directly in the FileInputStream ,my program runs quite finely and i was able to deserialise the object successsfully.But when i tried to use FileDialog,the serialisation program runs finely,but the deserialisation cannot be done. I am getting the following error:<\/p>\n<pre><code>java.io.FileNotFoundException: nullnull (The system cannot find the file specified)\nat java.io.FileInputStream.open(Native Method)\n    at java.io.FileInputStream.(Unknown Source)\n    at java.io.FileInputStream.(Unknown Source)\n    at package1.Deserialisation.main(Deserialisation.java:22)\nException in thread \"main\" java.lang.NullPointerException\n    at package1.Deserialisation.main(Deserialisation.java:43) .\n<\/code><\/pre>\n<p>Can any one help me to resolve the same ande also explain in breif the reason for it so that mistakes are not repeated..<\/p>\n<p>Following is my code :<\/p>\n<pre><code>       package package1;\n        public class Employee implements java.io.Serializable\n        {\n    private static final long serialVersionUID = 2L;\n\n    String name;\n    String address;\n    int age;\n    \/\/int SSN;\n    transient int SSN;    \n    public void checkDetails()\n    {\n        System.out.println(\"The check details for the employee\" + name + \"are :\");\n    }\n\n}\n\n         Serialisation:\n         package package1;\n        import java.awt.FileDialog;\n        import java.awt.Frame;\n        import java.io.FileOutputStream;\n        import java.io.IOException;\n        import java.io.ObjectOutputStream;\n\n        public class Serialization \n         {\n     public static void main(String[] args)\n     {\n          Employee emp =new Employee();\n          emp.name=\"vidhya\";\n          emp.address=\"XYZ\";\n          emp.age=26;\n          emp.SSN=123456;\n          try \n          {   \n            FileDialog fd =new FileDialog(new Frame(),\"Save As...\",FileDialog.SAVE);\n\n            fd.setVisible(true);\n            String filepath=new String (fd.getDirectory()+fd.getFile());\n            FileOutputStream fileout =new FileOutputStream(filepath);\n    ObjectOutputStream objout=new ObjectOutputStream(fileout);\n    objout.writeObject(emp);\n    fileout.close();\n    objout.close();\n    } \n          catch (IOException e2) {\n        e2.printStackTrace();\n        }\n            }\n       }\n\n\n    Deserialisation :\n         package package1;\n        import java.awt.FileDialog;\n        import java.awt.Frame;\n        import java.io.FileInputStream;\n        import java.io.IOException;\n       import java.io.ObjectInputStream;\n       public class Deserialisation \n       {\n        static Employee emp=null;\n    public static void main(String[] args) \n    {\n      try {  \n        FileDialog fd1 =new FileDialog(new Frame(),\"Open...\",FileDialog.LOAD);\n        String filepath = new String(fd1.getDirectory() + fd1.getFile());\n        FileInputStream filein =new FileInputStream(filepath);\n        ObjectInputStream objin =new ObjectInputStream(filein);\n        emp=(Employee) objin.readObject();\n        objin.close();\n        filein.close();\n        } \n\n        catch (IOException e) \n        {\n        e.printStackTrace();\n        } \n        catch (ClassNotFoundException e1)\n        {\n            System.out.println(\"Employee class not found\");\n            e1.printStackTrace();\n            return;       \n        }\n\n        System.out.println(\"Employee Name : \" + emp.name);\n        System.out.println(\"Address :\" + emp.address);\n        System.out.println(\"Age : \" + emp.age);\n        System.out.println(\"SSN :\" + emp.SSN);\n    }\n\n}\n<\/code><\/pre>\n<ol>\n<li>\n<pre><code>String filepath = new String(fd1.getDirectory() + fd1.getFile());\n<\/code><\/pre>\n<p>This is creating a path of &#8220;nullnull&#8221; meaning both these values are null. Try using <code>fd1.show();<\/code> before the line above.<\/p>\n<p>Edit: As pointed out by Viydha show() is deprecated and setVisible() should be used.<\/p>\n<\/li>\n<li>\n<p>The error is in your <code>FileDialog<\/code>. The line :<\/p>\n<pre><code>String filepath = new String (fd.getDirectory()+fd.getFile());\n<\/code><\/pre>\n<p>creates a path of <code>nullnull<\/code>. You should do a <code>fd.show();<\/code> before this line, or, better, use a <code>JFileChooser<\/code><\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-02 21:11:37. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I had been trying to do an object serialiation,deserialisation program.When i am giving the File Name directly in the FileInputStream ,my program runs quite finely and i was able to deserialise the object successsfully.But when i tried to use FileDialog,the serialisation program runs finely,but the deserialisation cannot be done. I am getting the following error: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1889","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1889","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1889"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1889\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}