When one writes an erroneous null datum, it would be helpful for the stack
trace to say more about what happened; for example:
java.lang.NullPointerException: MyRecord.myField
at ...
Caused by: java.lang.NullPointerException: string
at ...
Is someone working to implement something like this?
One can make a subclass of SpecificDatumWriter to do this, but one must
copy-n-paste GenericDatumWriter.writeRecord. It could be done without
copy-n-pasting, if GenericDatumWriter.writeRecord were split into two
methods:
protected void writeRecord(Schema schema, Object datum, Encoder out)
throws IOException {
for (Field field : schema.getFields()) {
writeField(schema, datum, field, out);
}
}
protected void writeField(Schema schema, Object datum, Field field,
Encoder out)
throws IOException {
write(field.schema(), getField(datum, field.name(), field.pos()), out);
}
Then the subclass could override two methods:
@Override
protected void writeField(Schema schema, Object datum, Field field,
Encoder out)
throws IOException {
try {
super.writeField(schema, datum, field, out);
} catch (NullPointerException e) {
NullPointerException e2 =
new NullPointerException(schema.getName() + "." + field.name());
e2.initCause(e);
throw e2;
}
}
@Override
protected void write(Schema schema, Object datum, Encoder out)
throws IOException {
switch (schema.getType()) {
case UNION:
case NULL:
break;
default:
if (datum == null)
throw new NullPointerException(schema.getName());
}
super.write(schema, datum, out);
}
- John Kristian