How do I serialize with square-wire

181 views Asked by At

Getting a NullPointerException when I try to serialize the result: java.lang.NullPointerException: Attempt to invoke virtual method 'byte[] com.squareup.wire.ProtoAdapter.encode(java.lang.Object)' on a null object reference

Tried using BufferedOutputStream before ObjectOutputStream
Tried searching for same error

Code used to serialize:
'Result' is a auto generated class using wire-compiler.
result.toString() returns a String (so not null really)
If the entire 'result' was to be saved to as plain text, it would make a 5MB file.

public static void serialize(File fullPath, Result result){
        try {
            // System.out.println(result.toString());
            FileOutputStream fileOut = new FileOutputStream(fullPath);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(result); // NullPointerException mentioned above
            out.close();
            fileOut.close();
        } catch (IOException i) {
            i.printStackTrace();
        }
    }

The Retrofit instance:

OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(1, TimeUnit.MINUTES)
                .readTimeout(1, TimeUnit.MINUTES)
                .retryOnConnectionFailure(true)
                .build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(client)
                .addConverterFactory(ProtoConverterFactory.createWithRegistry(ExtensionRegistry.newInstance()))
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        SchemaAPI schemaAPI = retrofit.create(SchemaAPI.class);

This are the contents of the file at fullpath: ’ 
which doesnt seem to be the entire result.
1

There are 1 answers

0
oceanman123 On BEST ANSWER
ArrayList<ITEMS> itemsList = new ArrayList<>();
FileOutputStream fileOut = new FileOutputStream(schema);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
for(Result.ITEMS item:result.items){
    itemsList.add(item.newBuilder().build());
}
out.writeObject(itemsList);
out.close();
fileOut.close();