Buggy code:
public class WalletPocket implements ConnectionEventListener , TransactionEventListener , Serializable , TransactionBag { private static final Logger log = LoggerFactory . getLogger ( WalletPocket . class ) ; private final ReentrantLock lock = Threading . lock ( "WalletPocket" ) ; private final CoinType coinType ; private String description ; @ Nullable private Sha256Hash lastBlockSeenHash ; private int lastBlockSeenHeight = - 1 ; private long lastBlockSeenTimeSecs = 0 ; @ VisibleForTesting final HashMap < Address , String > addressesStatus ; @ VisibleForTesting final transient ArrayList < Address > addressesSubscribed ; @ VisibleForTesting final transient ArrayList < Address > addressesPendingSubscription ; @ VisibleForTesting final transient HashMap < Address , AddressStatus > statusPendingUpdates ; @ VisibleForTesting final transient HashSet < Sha256Hash > fetchingTransactions ; @ VisibleForTesting final Map < Sha256Hash , Transaction > pending ; @ VisibleForTesting final Map < Sha256Hash , Transaction > unspent ; @ VisibleForTesting final Map < Sha256Hash , Transaction > spent ; @ VisibleForTesting final Map < Sha256Hash , Transaction > dead ; protected final Map < Sha256Hash , Transaction > transactions ; @ VisibleForTesting public SimpleHDKeyChain keys ; @ Nullable private transient BlockchainConnection blockchainConnection ; protected transient CoinSelector coinSelector = new DefaultCoinSelector ( ) ; private final transient CopyOnWriteArrayList < ListenerRegistration < WalletPocketEventListener > > listeners ; @ Nullable private transient Wallet wallet = null ; private Runnable saveLaterRunnable = new Runnable ( ) { @ Override public void run ( ) { } } ; private Runnable saveNowRunnable = new Runnable ( ) { @ Override public void run ( ) { } } ; private int TX_DEPTH_SAVE_THRESHOLD = 4 ; public WalletPocket ( DeterministicKey rootKey , CoinType coinType , @ Nullable KeyCrypter keyCrypter , @ Nullable KeyParameter key ) { } WalletPocket ( SimpleHDKeyChain keys , CoinType coinType ) { } public boolean isNew ( ) { } public Set < Transaction > getTransactions ( boolean includeDead ) { } public Iterable < WalletTransaction > getWalletTransactions ( ) { } private static void addWalletTransactionsToSet ( Set < WalletTransaction > txs , Pool poolType , Collection < Transaction > pool ) { } public void addWalletTransaction ( WalletTransaction wtx ) { } private void markNotOwnOutputs ( Transaction transaction ) { } private void addWalletTransaction ( Pool pool , Transaction tx , boolean save ) { } @ Nullable public Transaction getTransaction ( Sha256Hash hash ) { } public HashMap < Sha256Hash , Transaction > getTransactions ( HashSet < Sha256Hash > hashes ) { } public void refresh ( ) { } public CoinType getCoinType ( ) { } @ Nullable public KeyCrypter getKeyCrypter ( ) { } @ Nullable public Sha256Hash getLastBlockSeenHash ( ) { } public void setLastBlockSeenHash ( @ Nullable Sha256Hash lastBlockSeenHash ) { } public void setLastBlockSeenHeight ( int lastBlockSeenHeight ) { } public void setLastBlockSeenTimeSecs ( long timeSecs ) { } public long getLastBlockSeenTimeSecs ( ) { } @ Nullable public Date getLastBlockSeenTime ( ) { } public int getLastBlockSeenHeight ( ) { } public void setDescription ( String description ) { } public String getDescription ( ) { } public Coin getBalance ( ) { } public Coin getBalance ( boolean includeUnconfirmed ) { } public Coin getPendingBalance ( ) { } Coin getTxBalance ( Iterable < Transaction > txs , boolean toMe ) { lock . lock ( ) ; try { Coin value = Coin . ZERO ; for ( Transaction tx : txs ) { if ( toMe ) { <START_BUG> value = value . add ( tx . getValueSentToMe ( this ) ) ; <END_BUG> } else { value = value . add ( tx . getValue ( this ) ) ; } } return value ; } finally { lock . unlock ( ) ; } } public void setWallet ( Wallet wallet ) { } public Wallet getWallet ( ) { } public void broadcastTx ( Transaction tx ) throws IOException { } public void broadcastTx ( Transaction tx , TransactionEventListener listener ) throws IOException { } public boolean isConnected ( ) { } public WalletPocketConnectivity getConnectivityStatus ( ) { } @ VisibleForTesting boolean registerStatusForUpdate ( AddressStatus status ) { } void commitAddressStatus ( AddressStatus newStatus ) { } private boolean isAddressStatusChanged ( AddressStatus addressStatus ) { } @ Nullable public AddressStatus getAddressStatus ( Address address ) { } public List < AddressStatus > getAllAddressStatus ( ) { } @ VisibleForTesting List < Address > getAddressesToWatch ( ) { } private void confirmAddressSubscription ( Address address ) { } @ Override public void onNewBlock ( BlockHeader header ) { } private void maybeUpdateBlockDepth ( TransactionConfidence confidence ) { } @ Override public void onAddressStatusUpdate ( AddressStatus status ) { } @ Override public void onTransactionHistory ( AddressStatus status , List < HistoryTx > historyTxes ) { } @ Override public void onUnspentTransactionUpdate ( AddressStatus status , List < UnspentTx > unspentTxes ) { } private void tryToApplyState ( ) { } private void tryToApplyState ( AddressStatus status ) { } private void applyState ( AddressStatus status , HashMap < Sha256Hash , Transaction > txs ) { } private void markUnspentTXO ( AddressStatus status , Map < Sha256Hash , Transaction > txs ) { } private boolean isTxPending ( Transaction tx ) { } private void connectTransaction ( Transaction tx ) { } private void maybeMovePool ( Transaction tx ) { } private void maybeFlipSpentUnspent ( Transaction tx ) { } private void fetchTransactions ( List < ? extends HistoryTx > txes ) { } private void fetchTransactionIfNeeded ( Sha256Hash txHash ) { } private boolean isTransactionAvailableOrQueued ( Sha256Hash txHash ) { } private void addNewTransactionIfNeeded ( Transaction tx ) { } @ Override public void onTransactionUpdate ( Transaction tx ) { } @ Override public void onTransactionBroadcast ( Transaction tx ) { } @ Override public void onTransactionBroadcastError ( Transaction tx , Throwable throwable ) { } @ Override public void onConnection ( BlockchainConnection blockchainConnection ) { } @ Override public void onDisconnect ( ) { } private void subscribeToBlockchain ( ) { }

Fix:
value = value . add ( tx . getValueSentToMe ( this , false ) ) ;