#vulnerable code 
                                Map<String, SDChaindcode> discoverEndorserEndpoints(TransactionContext transactionContext, List<List<ServiceDiscoveryChaincodeCalls>> chaincodeNames) throws ServiceDiscoveryException {
        if (null == chaincodeNames) {
            logger.warn("Discover of chaincode names was null.");
            return Collections.emptyMap();
        }
        if (chaincodeNames.isEmpty()) {
            logger.warn("Discover of chaincode names was empty.");
            return Collections.emptyMap();
        }
        if (DEBUG) {
            StringBuilder cns = new StringBuilder(1000);
            String sep = "";
            cns.append("[");
            for (List<ServiceDiscoveryChaincodeCalls> s : chaincodeNames) {

                ServiceDiscoveryChaincodeCalls n = s.get(0);
                cns.append(sep).append(n.write(s.subList(1, s.size())));
                sep = ", ";
            }
            cns.append("]");
            logger.debug(format("Channel %s doing discovery for chaincodes: %s", channelName, cns.toString()));
        }

        ArrayList<Peer> speers = new ArrayList<>(serviceDiscoveryPeers);
        Collections.shuffle(speers);
        final Map<String, SDChaindcode> ret = new HashMap<>();
        SDNetwork sdNetwork = networkDiscovery(transactionContext, false);
        ServiceDiscoveryException serviceDiscoveryException = null;

        for (Peer serviceDiscoveryPeer : speers) {
            serviceDiscoveryException = null;
            try {
                logger.debug(format("Channel %s doing discovery for chaincodes on peer: %s", channelName, serviceDiscoveryPeer.toString()));

                TransactionContext ltransactionContext = transactionContext.retryTransactionSameContext();
                final byte[] clientTLSCertificateDigest = serviceDiscoveryPeer.getClientTLSCertificateDigest();

                if (null == clientTLSCertificateDigest) {
                    logger.warn(format("Channel %s peer %s requires mutual tls for service discovery.", channelName, serviceDiscoveryPeer.toString()));
                    continue;
                }

                ByteString clientIdent = ltransactionContext.getIdentity().toByteString();
                ByteString tlshash = ByteString.copyFrom(clientTLSCertificateDigest);
                Protocol.AuthInfo authentication = Protocol.AuthInfo.newBuilder().setClientIdentity(clientIdent).setClientTlsCertHash(tlshash).build();

                List<Protocol.Query> fq = new ArrayList<>(chaincodeNames.size());

                for (List<ServiceDiscoveryChaincodeCalls> chaincodeName : chaincodeNames) {
                    if (ret.containsKey(chaincodeName.get(0).getName())) {
                        continue;
                    }
                    LinkedList<Protocol.ChaincodeCall> chaincodeCalls = new LinkedList<>();
                    chaincodeName.forEach(serviceDiscoveryChaincodeCalls -> chaincodeCalls.add(serviceDiscoveryChaincodeCalls.build()));
                    List<Protocol.ChaincodeInterest> cinn = new ArrayList<>(1);
                    chaincodeName.forEach(ServiceDiscoveryChaincodeCalls::build);
                    Protocol.ChaincodeInterest cci = Protocol.ChaincodeInterest.newBuilder().addAllChaincodes(chaincodeCalls).build();
                    cinn.add(cci);
                    Protocol.ChaincodeQuery chaincodeQuery = Protocol.ChaincodeQuery.newBuilder().addAllInterests(cinn).build();

                    fq.add(Protocol.Query.newBuilder().setChannel(channelName).setCcQuery(chaincodeQuery).build());
                }

                if (fq.size() == 0) {
                    //this would be odd but lets take care of it.
                    break;
                }

                Protocol.Request request = Protocol.Request.newBuilder().addAllQueries(fq).setAuthentication(authentication).build();
                ByteString payloadBytes = request.toByteString();
                ByteString signatureBytes = ltransactionContext.signByteStrings(payloadBytes);
                Protocol.SignedRequest sr = Protocol.SignedRequest.newBuilder()
                        .setPayload(payloadBytes).setSignature(signatureBytes).build();
                if (IS_TRACE_LEVEL && null != diagnosticFileDumper) { // dump protobuf we sent
                    logger.trace(format("Service discovery channel %s %s service chaincode query sent %s", channelName, serviceDiscoveryPeer,
                            diagnosticFileDumper.createDiagnosticProtobufFile(sr.toByteArray())));
                }

                logger.debug(format("Channel %s peer %s sending chaincode query request", channelName, serviceDiscoveryPeer.toString()));
                final Protocol.Response response = serviceDiscoveryPeer.sendDiscoveryRequestAsync(sr).get(SERVICE_DISCOVERY_WAITTIME, TimeUnit.MILLISECONDS);
                if (IS_TRACE_LEVEL && null != diagnosticFileDumper) { // dump protobuf we get
                    logger.trace(format("Service discovery channel %s %s service chaincode query returned %s", channelName, serviceDiscoveryPeer,
                            diagnosticFileDumper.createDiagnosticProtobufFile(response.toByteArray())));
                }
                logger.debug(format("Channel %s peer %s completed chaincode query request", channelName, serviceDiscoveryPeer.toString()));
                serviceDiscoveryPeer.hasConnected();

                for (Protocol.QueryResult queryResult : response.getResultsList()) {
                    if (queryResult.getResultCase().getNumber() == Protocol.QueryResult.ERROR_FIELD_NUMBER) {
                        ServiceDiscoveryException discoveryException = new ServiceDiscoveryException(format("Error %s", queryResult.getError().getContent()));
                        logger.error(discoveryException.getMessage());
                        continue;
                    }

                    if (queryResult.getResultCase().getNumber() != Protocol.QueryResult.CC_QUERY_RES_FIELD_NUMBER) {
                        ServiceDiscoveryException discoveryException = new ServiceDiscoveryException(format("Error expected chaincode endorsement query but got %s : ", queryResult.getResultCase().toString()));
                        logger.error(discoveryException.getMessage());
                        continue;
                    }

                    Protocol.ChaincodeQueryResult ccQueryRes = queryResult.getCcQueryRes();
                    if (ccQueryRes.getContentList().isEmpty()) {
                        throw new ServiceDiscoveryException(format("Error %s", queryResult.getError().getContent()));
                    }

                    for (Protocol.EndorsementDescriptor es : ccQueryRes.getContentList()) {
                        final String chaincode = es.getChaincode();
                        List<SDLayout> layouts = new LinkedList<>();
                        for (Protocol.Layout layout : es.getLayoutsList()) {
                            SDLayout sdLayout = null;
                            Map<String, Integer> quantitiesByGroupMap = layout.getQuantitiesByGroupMap();
                            for (Map.Entry<String, Integer> qmap : quantitiesByGroupMap.entrySet()) {
                                final String key = qmap.getKey();
                                final int quantity = qmap.getValue();
                                if (quantity < 1) {
                                    continue;
                                }
                                Protocol.Peers peers = es.getEndorsersByGroupsMap().get(key);
                                if (peers == null || peers.getPeersCount() == 0) {
                                    continue;
                                }

                                List<SDEndorser> sdEndorsers = new LinkedList<>();

                                for (Protocol.Peer pp : peers.getPeersList()) {
                                    SDEndorser ppp = new SDEndorser(pp, null, null, asLocalhost);
                                    final String endPoint = ppp.getEndpoint();
                                    SDEndorser nppp = sdNetwork.getEndorserByEndpoint(endPoint);
                                    if (null == nppp) {
                                        sdNetwork = networkDiscovery(transactionContext, true);
                                        if (null == sdNetwork) {
                                            throw new ServiceDiscoveryException("Failed to discover network resources.");
                                        }
                                        nppp = sdNetwork.getEndorserByEndpoint(ppp.getEndpoint());
                                        if (null == nppp) {
                                            throw new ServiceDiscoveryException(format("Failed to discover peer endpoint information %s for chaincode %s ", endPoint, chaincode));
                                        }
                                    }
                                    sdEndorsers.add(nppp);
                                }
                                if (sdLayout == null) {
                                    sdLayout = new SDLayout();
                                    layouts.add(sdLayout);
                                }
                                sdLayout.addGroup(key, quantity, sdEndorsers);
                            }
                        }
                        if (layouts.isEmpty()) {
                            logger.warn(format("Channel %s chaincode %s discovered no layouts!", channelName, chaincode));
                        } else {
                            if (DEBUG) {
                                StringBuilder sb = new StringBuilder(1000);
                                sb.append("Channel ").append(channelName)
                                        .append(" found ").append(layouts.size()).append(" layouts for chaincode: ").append(es.getChaincode());
                                sb.append(", layouts: [");

                                String sep = "";
                                for (SDLayout layout : layouts) {
                                    sb.append(sep).append(layout);
                                    sep = ", ";
                                }
                                sb.append("]");

                                logger.debug(sb.toString());
                            }
                            ret.put(es.getChaincode(), new SDChaindcode(es.getChaincode(), layouts));
                        }
                    }
                }

                if (ret.size() == chaincodeNames.size()) {
                    break; // found them all.
                }
            } catch (ServiceDiscoveryException e) {
                logger.warn(format("Service discovery error on peer %s. Error: %s", serviceDiscoveryPeer.toString(), e.getMessage()));
                serviceDiscoveryException = e;
            } catch (Exception e) {
                logger.warn(format("Service discovery error on peer %s. Error: %s", serviceDiscoveryPeer.toString(), e.getMessage()));
                serviceDiscoveryException = new ServiceDiscoveryException(e);
            }
        }

        if (null != serviceDiscoveryException) {
            throw serviceDiscoveryException;
        }
        if (ret.size() != chaincodeNames.size()) {
            logger.warn((format("Channel %s failed to find all layouts for chaincodes. Expected: %d and found: %d", channelName, chaincodeNames.size(), ret.size())));
        }

        return ret;
    } 
                            #location 43 
                            #vulnerability type NULL_DEREFERENCE
#fixed code
                                ServiceDiscovery(Channel channel, Collection<Peer> serviceDiscoveryPeers, TransactionContext transactionContext) {
        this.serviceDiscoveryPeers = serviceDiscoveryPeers;
        this.channel = channel;
        this.channelName = channel.getName();
        this.transactionContext = transactionContext.retryTransactionSameContext();
    }