Uploaded image for project: 'Apache Avro'
  1. Apache Avro
  2. AVRO-1949

Not setting a non-null field in GenericRecord causes a NullPointer exception on Serialisation even though a default is defined in the schema

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Minor
    • Resolution: Duplicate
    • 1.8.1
    • None
    • java
    • None
    • Windows 7, JDK8

    Description

      As per the summary, I had a search through the existing JIRAs but couldn't find a match apologies if this is a duplicate:

      example schema:

      {"namespace": "example.data",
        "type": "record",
        "name": "Data",
        "fields": [
          {"name": "name", "type": "string"},
          {"name": "favorite_season", "type": "string", "default": "Autumn"}
        ]
      }
      
      GenericRecord record = new GenericData.Record(schema);
      record.put("name", "my name");
      

      Serialising the above throws a NullPointerException:

      java.lang.NullPointerException: null of string in field favorite_season of example.data.Data
      	at org.apache.avro.generic.GenericDatumWriter.npe(GenericDatumWriter.java:145) ~[classes/:?]
      	at org.apache.avro.generic.GenericDatumWriter.writeWithoutConversion(GenericDatumWriter.java:139) ~[classes/:?]
      	at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:75) ~[classes/:?]
      	at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:62) ~[classes/:?]
      

      Surely, given that the schema is available the writeField method should check to see if there is a default field available to use if the required field does not have a value supplied (and is not-null).

      Attachments

        Activity

          People

            Unassigned Unassigned
            mhurd Mike Hurd
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: