Buggy code:
public class KanvasingFinishAct extends Activity implements OnClickListener { @ Override protected void onCreate ( Bundle savedInstanceState ) { } private void initComp ( ) { } private TrnRoute getCurrTrnRoute ( String storeID ) { } private KanvasingFinishAct . FinishTransactionPriceList [ ] decodeJSON ( ) { } private long calculateTotal ( KanvasingFinishAct . FinishTransactionPriceList [ ] finishTransPL ) { } private void calculateOthers ( ) { } private void updateDatabase ( ) { int lastTrnSalesID = 0 ; Cursor cursor = DatabaseAdapter . instance ( getBaseContext ( ) ) . rawQuery ( ( "SELECT<seq2seq4repair_space>id<seq2seq4repair_space>" + "FROM<seq2seq4repair_space>mobile_trnsales<seq2seq4repair_space>" ) , null ) ; cursor . moveToFirst ( ) ; do { int temp = Integer . valueOf ( cursor . getString ( 0 ) ) ; if ( temp > lastTrnSalesID ) lastTrnSalesID = temp ; } while ( cursor . moveToNext ( ) ) ; cursor . close ( ) ; cursor = DatabaseAdapter . instance ( getBaseContext ( ) ) . rawQuery ( ( "SELECT<seq2seq4repair_space>unitcompany_code<seq2seq4repair_space>" + ( "FROM<seq2seq4repair_space>mobile_user<seq2seq4repair_space>" + "LIMIT<seq2seq4repair_space>1" ) ) , null ) ; cursor . moveToFirst ( ) ; User user = new User ( "dummy<seq2seq4repair_space>ID" ) ; user . mUnitCompanyCode = cursor . getString ( 0 ) ; cursor . close ( ) ; cursor = DatabaseAdapter . instance ( getBaseContext ( ) ) . rawQuery ( ( "SELECT<seq2seq4repair_space>code,<seq2seq4repair_space>counter<seq2seq4repair_space>" + ( "FROM<seq2seq4repair_space>mobile_counter<seq2seq4repair_space>" + "LIMIT<seq2seq4repair_space>1" ) ) , null ) ; cursor . moveToFirst ( ) ; Counter counter = new Counter ( "dummy<seq2seq4repair_space>ID" ) ; counter . mCode = cursor . getString ( 0 ) ; counter . mCounter = cursor . getLong ( 1 ) ; cursor . close ( ) ; String refno = ( ( ( ( counter . mCode ) + "//" ) + ( Utilities . formatDate ( new Date ( ) ) . substring ( 0 , 2 ) ) ) + "//" ) + ( String . valueOf ( counter . mCounter ) ) ; TrnSales newTrnSales = new TrnSales ( String . valueOf ( ( ++ lastTrnSalesID ) ) , user . mUnitCompanyCode , "01" , new Date ( ) , refno , "ar01" , ( "sales<seq2seq4repair_space>kanvas<seq2seq4repair_space>" + ( mCurrTrnRoute . mCustomerName ) ) , "checkNo01" , new Date ( ) , mCurrTrnRoute . mCustomerCode , "40115" , "SAT01" , mNettoNum , mNettoNum , 0 , 0 ) ; DatabaseAdapter . instance ( getBaseContext ( ) ) . insert ( TrnSales . getTableName ( ) , newTrnSales ) ; int lastDtlSalesID = 0 ; cursor = DatabaseAdapter . instance ( getBaseContext ( ) ) . rawQuery ( ( "SELECT<seq2seq4repair_space>id<seq2seq4repair_space>" + "FROM<seq2seq4repair_space>mobile_dtlsales<seq2seq4repair_space>" ) , null ) ; cursor . moveToFirst ( ) ; do { int temp = Integer . valueOf ( cursor . getString ( 0 ) ) ; <START_BUG> if ( temp > lastTrnSalesID ) <END_BUG> lastDtlSalesID = temp ; } while ( cursor . moveToNext ( ) ) ; cursor . close ( ) ; for ( int i = 0 ; i < ( mFinishTransPL . length ) ; ++ i ) { DtlSales newDtlSales = new DtlSales ( String . valueOf ( ( ++ lastDtlSalesID ) ) , String . valueOf ( lastTrnSalesID ) , mFinishTransPL [ i ] . mProductCode , mFinishTransPL [ i ] . mQuantity , 0 , mFinishTransPL [ i ] . mQuantity , mFinishTransPL [ i ] . mPrice , ( ( mFinishTransPL [ i ] . mQuantity ) * ( mFinishTransPL [ i ] . mPrice ) ) , ( ( mFinishTransPL [ i ] . mQuantity ) * ( mFinishTransPL [ i ] . mPrice ) ) , 0 , ( ( mFinishTransPL [ i ] . mQuantity ) * ( mFinishTransPL [ i ] . mPrice ) ) , 0 ) ; DatabaseAdapter . instance ( getBaseContext ( ) ) . insert ( DtlSales . getTableName ( ) , newDtlSales ) ; } } @ Override public void onClick ( View v ) { } private TextView mTotal ; private TextView mDiscount ; private TextView mNetto ; private TextView mPayment ; private TextView mCreditLimit ; private TextView mReceivable ; private Button mConfirmButton ; private String mStoreID ; private long mTotalNum ; private long mDiscNum ; private long mNettoNum ; private long mPaymentNum ; private TrnRoute mCurrTrnRoute ; private KanvasingFinishAct . FinishTransactionPriceList [ ] mFinishTransPL ; private class FinishTransactionPriceList { public FinishTransactionPriceList ( String productCode , long productQuantity , long price , long bonus ) { } public String mProductCode ; public long mQuantity ; public long mPrice ; public long mBonus ; } }

Fix:
if ( temp > lastDtlSalesID )