I defined my fields to be optional, i.e. with a schema of a union of null
and my proper type


{

    "name" : "Yytest",
    "namespace" : "com.yahoo.enc.avro",

    "type" : "record",

    "fields" : [
    { "name" : "f1" , "type" : [{ "type" : "array", "items" : "long" },
"null" ] },
    { "name" : "f2", "type" : [ "int", "null"] }
    ]

}


then
in java code, I tried to print out the record without assigning value to
either f1 or f2

        Yytest y = new Yytest();
        y.f2 = 1;
        y.f1 = new GenericData.Array(10, null);

        System.out.println(y.toString());



taking out either of the f1 or f2 assignments would lead to the following
error:
     [java] Exception in thread "main" java.lang.NullPointerException
     [java]     at
org.apache.avro.specific.SpecificRecordBase.hashCode(SpecificRecordBase.java:62)
     [java]     at
org.apache.avro.specific.SpecificRecordBase.hashCode(SpecificRecordBase.java:55)
     [java]     at java.lang.Object.toString(Object.java:219)
     [java]     at com.yahoo.enc.avro.AvroGdserve.main(AvroGdserve.java:259)
     [java] Java Result: 1






doesn't the "null" allow a field to be optional ?


Thanks
Yang

Reply via email to