Program: 67027
Project: 67027
Buggy source:
public void addGlobalElements(HashMap<QName,ElementDeclaration> additionalElements){
  for (  QName next : additionalElements.keySet()) {
    if (next == null) {
      break;
    }
    String namespaceURI=next.getNamespaceURI();
    Schema targetSchema=getSchemaForNamespace(namespaceURI);
    if (targetSchema == null) {
      break;
    }
    if (targetSchema.getTopLevelElements().get(next.getLocalPart()) == null) {
      Element element=new Element();
      element.setName(next.getLocalPart());
      ElementDeclaration nextElement=additionalElements.get(next);
      JavaClass javaClass=nextElement.getJavaType();
      QName schemaType=(QName)helper.getXMLToJavaTypeMap().get(javaClass.getRawName());
      if (schemaType != null) {
        element.setType(XMLConstants.SCHEMA_PREFIX + "String_Node_Str" + schemaType.getLocalPart());
      }
 else {
        TypeInfo type=(TypeInfo)this.typeInfo.get(javaClass.getQualifiedName());
        if (type != null) {
          String typeName=null;
          if (type.isComplexType()) {
            typeName=type.getComplexType().getName();
          }
 else {
            typeName=type.getSimpleType().getName();
          }
          if (type.getClassNamespace().equals(namespaceURI)) {
            element.setType(typeName);
          }
 else {
            Schema complexTypeSchema=getSchemaForNamespace(type.getClassNamespace());
            String complexTypeSchemaNS=type.getClassNamespace();
            if (complexTypeSchemaNS == null) {
              complexTypeSchemaNS="String_Node_Str";
            }
            addImportIfRequired(targetSchema,complexTypeSchema,type.getClassNamespace());
            String prefix=targetSchema.getNamespaceResolver().resolveNamespaceURI(complexTypeSchemaNS);
            if (prefix != null) {
              element.setType(prefix + "String_Node_Str" + typeName);
            }
 else {
              element.setType(typeName);
            }
          }
        }
      }
      if (nextElement.getSubstitutionHead() != null) {
        String subLocal=nextElement.getSubstitutionHead().getLocalPart();
        String subNamespace=nextElement.getSubstitutionHead().getNamespaceURI();
        String prefix=getPrefixForNamespace(subNamespace,targetSchema.getNamespaceResolver());
        if (prefix == null || prefix.equals("String_Node_Str")) {
          element.setSubstitutionGroup(subLocal);
        }
 else {
          element.setSubstitutionGroup(prefix + "String_Node_Str" + subLocal);
        }
      }
      targetSchema.addTopLevelElement(element);
      SchemaTypeInfo info=this.schemaTypeInfo.get(javaClass.getQualifiedName());
      if (info == null) {
        info=new SchemaTypeInfo();
        info.setSchemaTypeName(schemaType);
        schemaTypeInfo.put(javaClass.getQualifiedName(),info);
      }
      info.getGlobalElementDeclarations().add(next);
    }
  }
}

Fixed version (ground truth):
public void addGlobalElements(HashMap<QName,ElementDeclaration> additionalElements){
  for (  QName next : additionalElements.keySet()) {
    if (next != null) {
      ElementDeclaration nextElement=additionalElements.get(next);
      if (nextElement.getScopeClass() == GLOBAL.class) {
        String namespaceURI=next.getNamespaceURI();
        Schema targetSchema=getSchemaForNamespace(namespaceURI);
        if (targetSchema == null) {
          break;
        }
        if (targetSchema.getTopLevelElements().get(next.getLocalPart()) == null) {
          Element element=new Element();
          element.setName(next.getLocalPart());
          JavaClass javaClass=nextElement.getJavaType();
          QName schemaType=(QName)helper.getXMLToJavaTypeMap().get(javaClass.getRawName());
          if (schemaType != null) {
            element.setType(XMLConstants.SCHEMA_PREFIX + "String_Node_Str" + schemaType.getLocalPart());
          }
 else {
            TypeInfo type=(TypeInfo)this.typeInfo.get(javaClass.getQualifiedName());
            if (type != null) {
              String typeName=null;
              if (type.isComplexType()) {
                typeName=type.getComplexType().getName();
              }
 else {
                typeName=type.getSimpleType().getName();
              }
              if (type.getClassNamespace().equals(namespaceURI)) {
                element.setType(typeName);
              }
 else {
                Schema complexTypeSchema=getSchemaForNamespace(type.getClassNamespace());
                String complexTypeSchemaNS=type.getClassNamespace();
                if (complexTypeSchemaNS == null) {
                  complexTypeSchemaNS="String_Node_Str";
                }
                addImportIfRequired(targetSchema,complexTypeSchema,type.getClassNamespace());
                String prefix=targetSchema.getNamespaceResolver().resolveNamespaceURI(complexTypeSchemaNS);
                if (prefix != null) {
                  element.setType(prefix + "String_Node_Str" + typeName);
                }
 else {
                  element.setType(typeName);
                }
              }
            }
          }
          if (nextElement.getSubstitutionHead() != null) {
            String subLocal=nextElement.getSubstitutionHead().getLocalPart();
            String subNamespace=nextElement.getSubstitutionHead().getNamespaceURI();
            String prefix=getPrefixForNamespace(subNamespace,targetSchema.getNamespaceResolver());
            if (prefix == null || prefix.equals("String_Node_Str")) {
              element.setSubstitutionGroup(subLocal);
            }
 else {
              element.setSubstitutionGroup(prefix + "String_Node_Str" + subLocal);
            }
          }
          targetSchema.addTopLevelElement(element);
          SchemaTypeInfo info=this.schemaTypeInfo.get(javaClass.getQualifiedName());
          if (info == null) {
            info=new SchemaTypeInfo();
            info.setSchemaTypeName(schemaType);
            schemaTypeInfo.put(javaClass.getQualifiedName(),info);
          }
          info.getGlobalElementDeclarations().add(next);
        }
      }
    }
  }
}