2

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); (see below)

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

package lib;

public class Place extends lib.Entry { // A subclass.

    public String name;

    public Place(String name) {
        this.name = name;
    }

}

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? 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);

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.

2 Answers 2

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

Remove the declaration of name from Place class.

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();  
}

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

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

I want to use the name defined in Place, that was not the issue. I used Entry() instead of main(), as suggested by Ravi, and maintained String name; // Set by the subclass and it worked. But, as you pointed out, there is no reason to have both fields, it's better to have Entry(String name). And I did forgot to implement the Serializeble interface. Thank you for your answer.
Generally speaking, if you want this sort of behavior, it's better to use a method than to rely on the subclasser to remember to fill in the field. Instead of String name, use abstract String getName().
1

Your Entry#main() isn't getting called because Main#main() is your main() method now. You need to add a constructor that initializes the filename in your Entry class as

   public Entry() {
        this.filename = this.name + ".ser";
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.