#vulnerable code 
                              @Override
  public void handlePieceCompleted(final SharingPeer peer, Piece piece)
          throws IOException {
    final SharedTorrent torrent = peer.getTorrent();
    synchronized (torrent) {
      if (piece.isValid()) {
        // Make sure the piece is marked as completed in the torrent
        // Note: this is required because the order the
        // PeerActivityListeners are called is not defined, and we
        // might be called before the torrent's piece completion
        // handler is.
        torrent.markCompleted(piece);
        logger.debug("Completed download of {} from {}, now has {}/{} pieces.",
                new Object[]{
                        piece,
                        peer,
                        torrent.getCompletedPieces().cardinality(),
                        torrent.getPieceCount()
                });

        // Send a HAVE message to all connected peers
        PeerMessage have = PeerMessage.HaveMessage.craft(piece.getIndex());
        final String torrentHash = torrent.getHexInfoHash();
        for (SharingPeer remote : getConnectedPeers()) {
          if (remote.getTorrent().getHexInfoHash().equals(torrentHash))
            remote.send(have);
        }

        BitSet completed = new BitSet();
        completed.or(torrent.getCompletedPieces());
        completed.and(peer.getAvailablePieces());
        if (completed.equals(peer.getAvailablePieces())) {
          // send not interested when have no interested pieces;
          peer.send(PeerMessage.NotInterestedMessage.craft());
        }

      } else {
        logger.info("Downloaded piece #{} from {} was not valid ;-(. Trying another peer", piece.getIndex(), peer);
        peer.getPoorlyAvailablePieces().set(piece.getIndex());
      }

      if (torrent.isComplete()) {
        //close connection with all peers for this torrent
        logger.info("Download of {} complete.", torrent.getName());

        torrent.finish();

        try {
          this.announce.getCurrentTrackerClient(torrent)
                  .announceAllInterfaces(TrackerMessage.AnnounceRequestMessage.RequestEvent.COMPLETED, true, torrent);
        } catch (AnnounceException e) {
          logger.debug("unable to announce torrent {} on tracker {}", torrent, torrent.getAnnounce());
        }

      }
    }
  } 
                            #location 50 
                            #vulnerability type NULL_DEREFERENCE
#fixed code
                              @Override
  public void handlePieceCompleted(final SharingPeer peer, Piece piece)
          throws IOException {
    final SharedTorrent torrent = peer.getTorrent();
    final String torrentHash;
    synchronized (torrent) {
      torrentHash = torrent.getHexInfoHash();
      if (piece.isValid()) {
        // Make sure the piece is marked as completed in the torrent
        // Note: this is required because the order the
        // PeerActivityListeners are called is not defined, and we
        // might be called before the torrent's piece completion
        // handler is.
        torrent.markCompleted(piece);
        logger.debug("Completed download of {} from {}, now has {}/{} pieces.",
                new Object[]{
                        piece,
                        peer,
                        torrent.getCompletedPieces().cardinality(),
                        torrent.getPieceCount()
                });

        BitSet completed = new BitSet();
        completed.or(torrent.getCompletedPieces());
        completed.and(peer.getAvailablePieces());
        if (completed.equals(peer.getAvailablePieces())) {
          // send not interested when have no interested pieces;
          peer.send(PeerMessage.NotInterestedMessage.craft());
        }

      } else {
        logger.info("Downloaded piece #{} from {} was not valid ;-(. Trying another peer", piece.getIndex(), peer);
        peer.getPoorlyAvailablePieces().set(piece.getIndex());
      }

      if (torrent.isComplete()) {
        //close connection with all peers for this torrent
        logger.info("Download of {} complete.", torrent.getName());

        torrent.finish();

        try {
          this.announce.getCurrentTrackerClient(torrent)
                  .announceAllInterfaces(TrackerMessage.AnnounceRequestMessage.RequestEvent.COMPLETED, true, torrent);
        } catch (AnnounceException e) {
          logger.debug("unable to announce torrent {} on tracker {}", torrent, torrent.getAnnounce());
        }

      }
    }

    // Send a HAVE message to all connected peers
    PeerMessage have = PeerMessage.HaveMessage.craft(piece.getIndex());
    for (SharingPeer remote : getConnectedPeers()) {
      if (remote.getTorrent().getHexInfoHash().equals(torrentHash))
        remote.send(have);
    }
  }