0

当我尝试序列化结果时出现 NullPointerException:java.lang.NullPointerException: Attempt to invoke virtual method 'byte[] com.squareup.wire.ProtoAdapter.encode(java.lang.Object)' on an null object reference

在 ObjectOutputStream 之前尝试使用 BufferedOutputStream
尝试搜索相同的错误

用于序列化的代码:
“结果”是使用线编译器自动生成的类。
result.toString() 返回一个字符串(所以真的不是空的)
如果整个“结果”要保存为纯文本,它将生成一个 5MB 的文件。

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

改造实例:

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.
4

1 回答 1

0
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();
于 2019-06-29T07:06:40.123 回答