Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 2 down vote accepted
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.

share|improve this answer
    
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. –  Alex Aug 9 '13 at 1:09
    
@Alex. You're welcome :) –  Rohit Jain Aug 9 '13 at 1:10
    
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(). –  chrylis Aug 9 '13 at 2:01

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";
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.