简体   繁体   English

尝试将对象实例写入ObjectOutputStream时发生NullPointerException

[英]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:17stream.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.

相关问题 尝试在类之间写入 object 时 ObjectOutputStream.writeObject() 冻结 - ObjectOutputStream.writeObject() freezing when trying to write object between classes 尝试写入文件时出现NullPointerException - NullPointerException when trying to write to file NockPointerException当Mockito嘲笑ObjectOutputStream.writeObject吗? - NullPointerException when Mockito mocking ObjectOutputStream.writeObject? 重置ObjectOutputStream时,摆动:EDT NullPointerException - Swing: EDT NullPointerException when resetting ObjectOutputStream ObjectOutputStream无法写我的对象 - ObjectOutputStream can't write my object 使用ObjectOutputStream将列表中的对象写入文件 - Write Object from List to File, using ObjectOutputStream Java:尝试使用objectoutputstream / objectinputstream读取对象-崩溃 - Java: Trying to read object with objectoutputstream/objectinputstream - crashes 使用BufferedOutputStream时,ObjectOutputStream不写入对象 - ObjectOutputStream does not write objects when taking BufferedOutputStream 使用 ObjectOutputStream 将对象写入文本文件时获取不可读字符 - Getting unreadable characters when using ObjectOutputStream to write my object to a text file 尝试读取parcel ArrayList时出现NullPointerException <Object> - NullPointerException when trying to read parcel ArrayList<Object>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM