#vulnerable code 
                              @Override public void process(ClusterEvent event) throws Exception {
    LOG.info("START PersistAssignmentStage.process()");
    long startTime = System.currentTimeMillis();

    ClusterDataCache cache = event.getAttribute("ClusterDataCache");
    ClusterConfig clusterConfig = cache.getClusterConfig();

    if (clusterConfig.isPersistBestPossibleAssignment()) {
      HelixManager helixManager = event.getAttribute("helixmanager");
      HelixDataAccessor accessor = helixManager.getHelixDataAccessor();
      PropertyKey.Builder keyBuilder = accessor.keyBuilder();
      BestPossibleStateOutput bestPossibleAssignments =
          event.getAttribute(AttributeName.BEST_POSSIBLE_STATE.toString());
      Map<String, Resource> resourceMap = event.getAttribute(AttributeName.RESOURCES.toString());

      for (String resourceId : bestPossibleAssignments.resourceSet()) {
        Resource resource = resourceMap.get(resourceId);
        if (resource != null) {
          boolean changed = false;
          Map<Partition, Map<String, String>> bestPossibleAssignment =
              bestPossibleAssignments.getResourceMap(resourceId);
          IdealState idealState = cache.getIdealState(resourceId);
          if (idealState == null) {
            LOG.warn("IdealState not found for resource " + resourceId);
            continue;
          }
          IdealState.RebalanceMode mode = idealState.getRebalanceMode();
          if (!mode.equals(IdealState.RebalanceMode.SEMI_AUTO) && !mode
              .equals(IdealState.RebalanceMode.FULL_AUTO)) {
            // do not persist assignment for resource in neither semi or full auto.
            continue;
          }

          //TODO: temporary solution for Espresso/Dbus backcompatible, should remove this.
          Map<Partition, Map<String, String>> assignmentToPersist =
              convertAssignmentPersisted(resource, idealState, bestPossibleAssignment);

          for (Partition partition : resource.getPartitions()) {
            Map<String, String> instanceMap = assignmentToPersist.get(partition);
            Map<String, String> existInstanceMap =
                idealState.getInstanceStateMap(partition.getPartitionName());
            if (instanceMap == null && existInstanceMap == null) {
              continue;
            }
            if (instanceMap == null || existInstanceMap == null || !instanceMap
                .equals(existInstanceMap)) {
              changed = true;
              break;
            }
          }
          if (changed) {
            for (Partition partition : assignmentToPersist.keySet()) {
              Map<String, String> instanceMap = assignmentToPersist.get(partition);
              idealState.setInstanceStateMap(partition.getPartitionName(), instanceMap);
            }
            accessor.setProperty(keyBuilder.idealStates(resourceId), idealState);
          }
        }
      }
    }

    long endTime = System.currentTimeMillis();
    LOG.info("END PersistAssignmentStage.process(), took " + (endTime - startTime) + " ms");
  } 
                            #location 16 
                            #vulnerability type NULL_DEREFERENCE
#fixed code
                              @Override public void process(ClusterEvent event) throws Exception {
    LOG.info("START PersistAssignmentStage.process()");
    long startTime = System.currentTimeMillis();

    ClusterDataCache cache = event.getAttribute("ClusterDataCache");
    ClusterConfig clusterConfig = cache.getClusterConfig();

    if (!clusterConfig.isPersistBestPossibleAssignment()) {
      return;
    }

    BestPossibleStateOutput bestPossibleAssignment =
        event.getAttribute(AttributeName.BEST_POSSIBLE_STATE.name());

    HelixManager helixManager = event.getAttribute("helixmanager");
    HelixDataAccessor accessor = helixManager.getHelixDataAccessor();
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    Map<String, Resource> resourceMap = event.getAttribute(AttributeName.RESOURCES.name());

    for (String resourceId : bestPossibleAssignment.resourceSet()) {
      Resource resource = resourceMap.get(resourceId);
      if (resource != null) {
        final IdealState idealState = cache.getIdealState(resourceId);
        if (idealState == null) {
          LOG.warn("IdealState not found for resource " + resourceId);
          continue;
        }
        IdealState.RebalanceMode mode = idealState.getRebalanceMode();
        if (!mode.equals(IdealState.RebalanceMode.SEMI_AUTO) && !mode
            .equals(IdealState.RebalanceMode.FULL_AUTO)) {
          // do not persist assignment for resource in neither semi or full auto.
          continue;
        }

        boolean needPersist = false;
        if (mode.equals(IdealState.RebalanceMode.FULL_AUTO)) {
          // persist preference list in ful-auto mode.
          Map<String, List<String>> newLists =
              bestPossibleAssignment.getPreferenceLists(resourceId);
          if (newLists != null && hasPreferenceListChanged(newLists, idealState)) {
            idealState.setPreferenceLists(newLists);
            needPersist = true;
          }
        }

        Map<Partition, Map<String, String>> bestPossibleAssignements =
            bestPossibleAssignment.getResourceMap(resourceId);

        if (bestPossibleAssignements != null && hasInstanceMapChanged(bestPossibleAssignements,
            idealState)) {
          for (Partition partition : bestPossibleAssignements.keySet()) {
            Map<String, String> instanceMap = bestPossibleAssignements.get(partition);
            idealState.setInstanceStateMap(partition.getPartitionName(), instanceMap);
          }
          needPersist = true;
        }

        if (needPersist) {
          accessor.setProperty(keyBuilder.idealStates(resourceId), idealState);
        }
      }
    }

    long endTime = System.currentTimeMillis();
    LOG.info("END PersistAssignmentStage.process(), took " + (endTime - startTime) + " ms");
  }