Uploaded image for project: 'Solr'
  1. Solr
  2. SOLR-9112

Null Pointer Exception if @Field (child = true) is annotated at getter which takes collection of child docs

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • 6.0
    • None
    • clients - java
    • None

    Description

      See: http://stackoverflow.com/questions/37241489/solrj-6-0-0-insertion-of-a-bean-object-which-associate-list-of-bean-object-is-g
      for defect description.

      Defect is caused by below piece of code if annotation is present at setter for list of child:

      public DocField(AccessibleObject member) {
      if (member instanceof java.lang.reflect.Field)

      { field = (java.lang.reflect.Field) member; // It is null here }

      else

      { setter = (Method) member; // initialized as annotation is at setter }

      annotation = member.getAnnotation(Field.class);
      storeName(annotation);
      storeType(); // giving null pointer exception as field is null see below

      // Look for a matching getter
      if (setter != null) {
      String gname = setter.getName();
      if (gname.startsWith("set")) {
      gname = "get" + gname.substring(3);
      try

      { getter = setter.getDeclaringClass().getMethod(gname, (Class[]) null); }

      catch (Exception ex) {
      // no getter – don't worry about it...
      if (type == Boolean.class) {
      gname = "is" + setter.getName().substring(3);
      try

      { getter = setter.getDeclaringClass().getMethod(gname, (Class[]) null); }

      catch(Exception ex2)

      { // no getter -- don't worry about it... }

      }
      }
      }
      }
      }

      private void storeType() {
      if (field != null)

      { type = field.getType(); }

      else {
      Class[] params = setter.getParameterTypes();
      if (params.length != 1)

      { throw new BindingException("Invalid setter method. Must have one and only one parameter"); }

      type = params[0];
      }

      if (type == Collection.class || type == List.class || type == ArrayList.class) {
      isList = true;
      if (annotation.child())

      { populateChild(field.getGenericType()); //giving null pointer exception //as field is null here }

      else

      { type = Object.class; }

      } else if (type == byte[].class)

      { //no op }

      else if (type.isArray()) {
      isArray = true;
      if (annotation.child())

      { populateChild(type.getComponentType()); }

      else

      { type = type.getComponentType(); }

      } else if (type == Map.class || type == HashMap.class) { //corresponding to the support for dynamicFields
      if (annotation.child()) throw new BindingException("Map should is not a valid type for a child document");
      isContainedInMap = true;
      //assigned a default type
      type = Object.class;
      if (field != null) {
      if (field.getGenericType() instanceof ParameterizedType) {
      //check what are the generic values
      ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
      Type[] types = parameterizedType.getActualTypeArguments();
      if (types != null && types.length == 2 && types[0] == String.class) {
      //the key should always be String
      //Raw and primitive types
      if (types[1] instanceof Class) {
      //the value could be multivalued then it is a List, Collection, ArrayList
      if (types[1] == Collection.class || types[1] == List.class || types[1] == ArrayList.class)

      { type = Object.class; isList = true; } else { //else assume it is a primitive and put in the source type itself type = (Class) types[1]; }
      } else if (types[1] instanceof ParameterizedType) { //Of all the Parameterized types, only List is supported
      Type rawType = ((ParameterizedType) types[1]).getRawType();
      if (rawType == Collection.class || rawType == List.class || rawType == ArrayList.class) { type = Object.class; isList = true; }

      } else if (types[1] instanceof GenericArrayType)

      { //Array types type = (Class) ((GenericArrayType) types[1]).getGenericComponentType(); isArray = true; }

      else

      { //Throw an Exception if types are not known throw new BindingException("Allowed type for values of mapping a dynamicField are : " + "Object, Object[] and List"); }

      }
      }
      }
      } else {
      if (annotation.child())

      { populateChild(type); }

      }
      }

      Attachments

        Activity

          People

            Unassigned Unassigned
            sanjay.madnani Sanjay Madnani
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: