1

For Reference: I'm trying to use this library: https://github.com/danshannon/javastravav3api

For an API OAuth token, the library has no function to store the token perpetually. I tried to just make a class that extends Token and makes it implement Serialization to have perpetual tokens, but whenever I read the token all the fields are null. I checked to make sure the fields are not null right before writing the objects.

public class SerialToken extends Token implements Serializable
{
    public SerialToken() {
    }

    public SerialToken(TokenResponse tokenResponse, AuthorisationScope... scopes) {
        super(tokenResponse, scopes);
    }
}
public class Main {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
    // write your code here
        File f = new File("lastToken.txt");
        SerialToken token;
        if (!f.exists()) {
            AuthorisationAPI auth = API.authorisationInstance();
            TokenResponse response = auth.tokenExchange(/*credentials*/);
            token = new SerialToken(response);
            //System.out.println(token.getAthlete().getCity()); used to double check object isnt null going in (which it isnt)
            FileOutputStream fos = new FileOutputStream(f);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(token);
        }
        else
        {
            FileInputStream fis = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(fis);
            token = (SerialToken) ois.readObject();
        }

        System.out.println(token.getAthlete().getCity());
      
    }
}

Once I run it the first time to initialize my tokens it works perfectly, but whenever I run it a second time I get a null pointer exception from token.getAthlete(), where the token is not null but all its attributes (including the Athelete attribute) are null.

Thanks!

Shyam Baitmangalkar
  • 1,075
  • 12
  • 18

1 Answers1

2

I went through the project, the Token class (as well as the athlete classes) does not implement Serializable.

You're extending Token via SerialToken, and Token isn't Serializable while its subclass is. What gives?

According to the Serializable javadoc:

During deserialization, the fields of non-serializable classes will be initialized using the public or protected no-arg constructor of the class. A no-arg constructor must be accessible to the subclass that is serializable. The fields of serializable subclasses will be restored from the stream.

So basically, what happens when a class is serializable but its superclass is not ?

Well at the time of de-serialization, if a non-serializable superclass is present, then JVM will do nothing more with it than executing the default (no-arg) constructor of that class.

From the Athlete class in the project, you'll notice City is a private String variable, and its default value will hence be null.

So when you deserialise it, you get back a null (since the no-arg constructor doesn't provide it a non-null value either). And then calling getAthlete().getCity() would hence result in NullPointerException.

user207421
  • 305,947
  • 44
  • 307
  • 483
Ayush
  • 1,510
  • 11
  • 27