Java 如何使用readObject()从文件中读取复合类对象的数据?
如何使用readObject()从文件中读取复合对象的数据? 我的代码如下,我得到的输出为Java 如何使用readObject()从文件中读取复合类对象的数据?,java,Java,如何使用readObject()从文件中读取复合对象的数据? 我的代码如下,我得到的输出为 version=4 purpose is DEMO value=4 Exception in thread "main" java.lang.NullPointerException at Serializations.readObj(PS_Task3.java:39) at PS_Task3.main(PS_Task3.java:46) Press any key to
version=4
purpose is DEMO
value=4
Exception in thread "main" java.lang.NullPointerException
at Serializations.readObj(PS_Task3.java:39)
at PS_Task3.main(PS_Task3.java:46)
Press any key to continue . . .
//PS_Task3.java
//这是我试图执行的代码。当我在SerializationDemo中删除demo类型的变量时,得到了正确的输出。但当我尝试复合类时出现了NullPointerException。
请帮帮我,先谢谢你
import java.io.*;
class SerializationDemo implements Serializable //composite class
{
public int value = 4;
public demo d1;
public String purpose="DEMO";
public int getValue(){
return value;
}
}
class demo{
private int demoval;
demo(){
demoval=10;
}
public int getValue(){
return demoval;
}
}
class Serializations {
void writeObj()throws IOException{ //writes into file
FileOutputStream fos = new FileOutputStream("temp1.bin");
ObjectOutputStream oos = new ObjectOutputStream(fos);
SerializationDemo sd = new SerializationDemo();
oos.writeObject(sd);
oos.flush();
oos.close();
}
void readObj()throws IOException,ClassNotFoundException{ //reads from file
FileInputStream fis = new FileInputStream("temp1.bin");
ObjectInputStream oin = new ObjectInputStream(fis);
SerializationDemo sd = (SerializationDemo) oin.readObject();
System.out.println("version="+sd.value);
System.out.println("purpose is "+sd.purpose);
System.out.println("value="+sd.getValue());
System.out.println("value="+sd.d1.getValue());
}
}
public class PS_Task3{
public static void main(String args[]) throws IOException,ClassNotFoundException {
Serializations s = new Serializations();
s.writeObj();
s.readObj(); //here i got the error
}
}
请检查是否存在任何
null
void readObj()throws IOException,ClassNotFoundException{ //reads from file
try{
FileInputStream fis = new FileInputStream("temp1.bin");
System.out.println("1->"+fis==null);
ObjectInputStream oin = new ObjectInputStream(fis);
System.out.println("2->"+oin==null);
SerializationDemo sd = (SerializationDemo) oin.readObject();
System.out.println("3->"+sd==null);
System.out.println("version="+sd.value);
System.out.println("purpose is "+sd.purpose);
System.out.println("value="+sd.getValue());
System.out.println("value="+sd.d1.getValue());
}catch(Exception e)
{
System.out.println(e.toString);}
}
请检查where是否实际为null,然后在那里执行操作。要使序列化机制工作,所有类的本地成员变量必须是基元类型或可序列化对象 第一个问题是
SerializationDemo
有一个类型为demo
的成员变量不可序列化
这将解决该问题:
class demo implements Serializable{
private int demoval;
demo(){
demoval=10;
}
public int getValue(){
return demoval;
}
}
导致null异常的第二个问题是您从未设置d1
的值。确保在序列化它之前设置了它,否则当您尝试反序列化它时,您将尝试从null创建演示类的实例。简单的解决方法是添加默认构造函数:
import java.io.*;
class SerializationDemo implements Serializable //composite class
{
public SerializationDemo()
{
d1 = new demo();
}
public int value = 4;
public demo d1;
public String purpose="DEMO";
public int getValue(){
return value;
}
}