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

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

本文介绍了尝试将对象实例写入ObjectOutputStream时发生NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要

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)

Entry.java:17stream.writeObject(this);的位置(请参见下文)

Where Entry.java:17 is 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();
    }

}

为什么使用this时会得到NullPointerException?我正在尝试将当前对象实例写入ObjectOutputStream.我是Java的新手,我不知道如何进行.在Python中,我将使用stream.writeObject(self)之类的东西,因此出于同样的思路,我在Java中使用了this.我尝试使用stream.writeObject(Object this);,但是没有用.我也尝试过

Why am I getting a NullPointerException when using this? I'm trying to write the current object instance to the ObjectOutputStream. I'm new to Java and I have no idea how to proceed. In Python I'd use something like stream.writeObject(self) so by the same line of thought I used this in Java. I tried using stream.writeObject(Object this);, but it didn't work. I also tried

Object p = this;
stream.writeObject(p);

我想这是同一回事.它也没有用.这个想法是要有更多的类(除了Place之外)扩展Entry,从而允许使用Entry.serialize()方法对其进行序列化.

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.

推荐答案

String name; // Set by the subclass.

就是这个问题.由于您已经在子类中重新定义了name字段,因此它将不再使用this.name在超类中设置该字段. Place类中的name字段遮盖了Entry类中声明的字段.

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. The name field in Place class shadows the field declared in Entry class.

Place类中删除name的声明.

然后用参数化构造函数替换Entry类中的main()方法.我不知道您为什么首先拥有它.你甚至都没有打电话.

Then replace the main() method in Entry class with a parameterized constructor. 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();
}

然后从Place构造函数中调用超类构造函数,而不是直接设置字段:

then call the super class constructor from Place constructor, instead of setting the field directly:

public Place(String name) {
    super(name);
}

最后,使您的Place类实现Serializable接口.记住,您正在序列化它的实例.

And finally, make your Place class to implement Serializable interface. Remember, you are serializing it's instance.

这篇关于尝试将对象实例写入ObjectOutputStream时发生NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:51