#vulnerable code 
                              @Override
  public void handlePieceCompleted(final SharingPeer peer, Piece piece)
          throws IOException {
    final SharedTorrent torrent = peer.getTorrent();
    final String torrentHash = torrent.getHexInfoHash();
    if (piece.isValid()) {
      // 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);
      }
    }
    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()
                });

        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.debug("Download of {} complete.", torrent.getName());

        torrent.finish();

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

      }
    }
  } 
                            #location 51 
                            #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 = torrent.getHexInfoHash();
    if (piece.isValid()) {
      // 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);
      }
    }
    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()
                });

        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.debug("Download of {} complete.", torrent.getDirectoryName());

        torrent.finish();

        AnnounceableTorrent announceableTorrent = torrentsStorage.getAnnounceableTorrent(torrentHash);

        if (announceableTorrent == null) return;

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

      }
    }
  }