2017-07-27 69 views
-3

我正在学习Java并运行一些问题来恢复对象中的序列化数据。我编写这段代码的方法和尝试适用于另一个项目的做法一样,但对象数据返回为null。序列化对象返回null Java

输出示例: 序列化数据保存在item.ser中 文件存在!!! 反序列化项目...... 项目@ 7ef20235 SKU:0 名称:空 价格:0.0 征税:空 数量:0

import java.io.*; 
public class demo { 


    public static void main(String[] args) { 
     item i = new item("1234", "Guarana", 0.94, true, 1); 


     try { 



      FileOutputStream fileOut = 
        new FileOutputStream("item.ser"); 
      ObjectOutputStream out = new ObjectOutputStream(fileOut); 
      out.writeObject(i); 
      out.close(); 
      fileOut.close(); 
      System.out.println("Serialized data is saved in item.ser"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 


     try { 
     File F = new File("item.ser"); 
     if (F.exists() && !F.isDirectory()) { 
      System.out.println("File Exist!!!"); 
     } 
      FileInputStream fileIn = new FileInputStream("item.ser"); 
      ObjectInputStream in = new ObjectInputStream(fileIn); 
      item restore= (item) in.readObject(); 
      in.close(); 
      fileIn.close(); 
      System.out.println("Deserialized item..."); 
      System.out.println(restore); 
      System.out.println("Sku: "+restore.Sku); 
      System.out.println("Name: "+ restore.getName()); 
      System.out.println("Price: "+restore.Price); 
     /* if(ret.tax.toString()=="true"){ 
      System.out.println("Item is taxed"); 

     }else{ 
      System.out.println("Item is not taxed"); 
     }*/ 
      System.out.println("Is taxed: "+restore.tax); 
      System.out.println("Quantity: "+restore.qty); 
     } catch (IOException err) { 
      err.printStackTrace(); 
      return; 
     } catch (ClassNotFoundException c) { 
      System.out.println("Employee class not found"); 
      c.printStackTrace(); 
      return; 
     } 





    } 

} 

//class item 

import java.io.Serializable; 

/** 
* Created by diogowatson on 26/07/17. 
*/ 
public class item implements Serializable{ 

    String PorN; 
    int Sku; 
    String Name; 
    double Price; 
    Boolean tax; 
    int qty; 

    public item(String s, String apple, double v, boolean b, int i) { 
    } 

    public void item(){} 
    public void item (String p, int s, String n, double pr, boolean t, int q){ 
     PorN=p; 
     Sku=s; 
     Name=n; 
     Price=pr; 
     tax=t; 
     qty=q; 
    } 
public int getSku(){ 
     return Sku; 
} 
    public String getName(){ 
     return Name; 
} 

    public void PrinItem(){ 
     System.out.println("Sku: "+ Sku); 
     System.out.println("name: "+ Name); 
     System.out.println("Price: "+Price); 
     System.out.println("Is taxed: "+ tax); 
     System.out.println("Quantity: "+qty); 
    } 
} 
+3

编码不干净。类名和方法名相同。应该在'class'名字中首先输入大写字母。并且您的构造函数不会传递对象字段上的任何内容。这就是为什么你得到空。 – msagala25

+1

'item @ 7ef20235 Sku:0名称:null价格:0.0征税:null数量:0'不为空。它是'item'类型的对象,其*内容*为空。 – EJP

+0

@DiogoAndrade如果用户回答你的问题,也请接受他的回答([接受答案:它是如何工作的?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-工作))。如果不是,请说明还没有答案,这是StackOverflow非常重要的一部分,非常感谢。 – Zabuza

回答

2

这是相当简单的问题有没有关系序列化本身。您创建

item i = new item("1234", "Guarana", 0.94, true, 1); 

它调用你的item类的构造函数对象。这是你的这种方法:

public item(String s, String apple, double v, boolean b, int i) { 

} 

正如你所看到的,它没有设置任何东西。参数不会传递到其内部字段,因此整个item对象是空的。请尝试以下,你直接明白我的意思:

item i = new item("1234", "Guarana", 0.94, true, 1); 
System.out.println(i.Name) // prints null 

但是你可能有点糊涂了,你宣布:

public void item (String p, int s, String n, double pr, boolean t, int q){ 
    ... 
} 

有你其实正确设置应有尽有。 这是一个方法而不是构造你的item(你会在哪里iitem i = new item(...)设置例如称这样的方法,通过i.item(...))。


这是确切的原因命名约定建议用大写字母和方法的名称以小写字母(开头的类名也是可变/字段名应该以小写字母开头),所以通常你会称它为:

// Class 
public class Item { 
    // Field 
    String name; 

    // Constructor 
    public Item(...) { 
     ... 
    } 

    // Method 
    public void item(...) { 
     ... 
    } 
} 

所以,你需要做什么来解决这个问题的唯一的事情就是移动你的方法的内容纳入构造函数

+2

OP的课程:调试时始终要简单。在这种情况下,在调查为什么你的对象没有正确序列化之前,你应该确认它在第一种情况下是正确构建的。否则就像试图建造一座房子而不确定你是否在流沙中建造。 – yshavit

+1

非常感谢Zabuza!你给了我一个清晰,简明的答案,并给了我提示如何改进我的代码。这是一个愚蠢的错误,但有时我们需要别人来看看我们的代码,并显示一些明显的东西。 –

+1

不客气:)如果您的问题已经解决,请考虑**接受**其中一个答案。这有助于从未回答的问题中筛选出您的问题。 – Zabuza