[英]NullPointerException when trying to write object instance to ObjectOutputStream
I'm getting 我越来越
Exception in thread "main" java.lang.NullPointerException
at java.io.FileOutputStream.<init>(FileOutputStream.java:201)
at java.io.FileOutputStream.<init>(FileOutputStream.java:99)
at lib.Entry.serialize(Entry.java:17)
at main.Main.main(Main.java:8)
Where Entry.java:17
is stream.writeObject(this);
其中
Entry.java:17
是stream.writeObject(this);
(see below) (见下文)
Entry.java Entry.java
package lib;
import java.io.*;
public class Entry { // Superclass.
String filename; // Set below.
String name; // Set by the subclass.
public void main() {
this.filename = this.name + ".ser";
serialize();
}
public void serialize() {
try {
FileOutputStream file = new FileOutputStream(this.filename);
ObjectOutputStream stream = new ObjectOutputStream(file);
stream.writeObject(this);
stream.close();
file.close();
System.out.println("Serialized.");
} catch(IOException e) {
e.printStackTrace();
}
}
}
Place.java Place.java
package lib;
public class Place extends lib.Entry { // A subclass.
public String name;
public Place(String name) {
this.name = name;
}
}
Main.java Main.java
package main;
import lib.Place;
public abstract class Main {
public static void main(String[] args) {
Place room = new Place("room");
room.serialize();
}
}
Why am I getting a NullPointerException
when using this
? 为什么在使用
this
时会出现NullPointerException
? I'm trying to write the current object instance to the ObjectOutputStream
. 我正在尝试将当前对象实例写入
ObjectOutputStream
。 I'm new to Java and I have no idea how to proceed. 我是Java的新手,我不知道如何进行。 In Python I'd use something like
stream.writeObject(self)
so by the same line of thought I used this
in Java. 在Python中,我将使用类似
stream.writeObject(self)
类的东西,因此stream.writeObject(self)
同样的思路,我在Java中使用了this
。 I tried using stream.writeObject(Object this);
我尝试使用
stream.writeObject(Object this);
, but it didn't work. ,但无效。 I also tried
我也试过
Object p = this;
stream.writeObject(p);
Which I guess is the same thing. 我想那是同一回事。 It also didn't work.
它也没有用。 The idea is to have more classes (other than
Place
) extending Entry
, allowing them to be serialized using the Entry.serialize()
method. 这个想法是要有更多的类(不是
Place
)来扩展Entry
,从而允许使用Entry.serialize()
方法对它们进行序列化。
String name; // Set by the subclass.
That's the problem. 那就是问题所在。 Since you have re-defined the
name
field in subclass, it will no longer set the field in the super class using this.name
. 由于您已经在子类中重新定义了
name
字段,因此它将不再使用this.name
在超类中设置该字段。 The name
field in Place
class shadows the field declared in Entry
class. Place
类中的name
字段遮盖了Entry
类中声明的字段。
Remove the declaration of name
from Place
class. 从
Place
类中删除name
声明。
Then replace the main()
method in Entry
class with a parameterized constructor. 然后用参数化的构造函数替换
Entry
类中的main()
方法。 I don't know why you had it in the first place. 我不知道您为什么首先拥有它。 You aren't even calling it.
你甚至都没有打电话。
public Entry(String name) {
this.name = name;
this.filename = this.name + ".ser";
// Don't call it here. Your object hasn't been fully constructed yet.
// serialize();
}
then call the super class constructor from Place
constructor, instead of setting the field directly: 然后从
Place
构造函数调用超类构造函数,而不是直接设置字段:
public Place(String name) {
super(name);
}
And finally, make your Place
class to implement Serializable
interface. 最后,使
Place
类实现Serializable
接口。 Remember, you are serializing it's instance. 记住,您正在序列化它的实例。
Your Entry#main()
isn't getting called because Main#main()
is your main()
method now. 您的
Entry#main()
不会被调用,因为Main#main()
现在是您的main()
方法。 You need to add a constructor that initializes the filename in your Entry
class as 您需要添加一个构造函数,用于将
Entry
类中的文件名初始化为
public Entry() {
this.filename = this.name + ".ser";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.