@@ -114,12 +114,22 @@ public Split buildSplitInstance(Cursor cursor){
114114 * @return {@link org.gnucash.android.model.Split} instance
115115 */
116116 public Split getSplit (String uid ){
117- long id = getID (uid );
117+ return getSplit (getID (uid ));
118+ }
119+
120+ /**
121+ * Returns the Split instance given the database id
122+ * @param id Database record ID of the split
123+ * @return {@link org.gnucash.android.model.Split} instance
124+ */
125+ public Split getSplit (long id ){
118126 Cursor cursor = fetchRecord (id );
119127
120128 Split split = null ;
121- if (cursor != null && cursor .moveToFirst ()){
122- split = buildSplitInstance (cursor );
129+ if (cursor != null ) {
130+ if (cursor .moveToFirst ()) {
131+ split = buildSplitInstance (cursor );
132+ }
123133 cursor .close ();
124134 }
125135 return split ;
@@ -233,14 +243,18 @@ public Cursor fetchSplits(String condition, String sortOrder){
233243 * @return Database record ID of split
234244 */
235245 public long getID (String uid ){
246+ if (uid == null )
247+ return 0 ;
248+
236249 Cursor cursor = mDb .query (SplitEntry .TABLE_NAME ,
237250 new String [] {SplitEntry ._ID },
238251 SplitEntry .COLUMN_UID + " = ?" , new String []{uid }, null , null , null );
239252 long result = -1 ;
240- if (cursor != null && cursor .moveToFirst ()){
241- Log .d (TAG , "Transaction already exists. Returning existing id" );
242- result = cursor .getLong (cursor .getColumnIndexOrThrow (SplitEntry ._ID ));
243-
253+ if (cursor != null ){
254+ if (cursor .moveToFirst ()) {
255+ Log .d (TAG , "Transaction already exists. Returning existing id" );
256+ result = cursor .getLong (cursor .getColumnIndexOrThrow (SplitEntry ._ID ));
257+ }
244258 cursor .close ();
245259 }
246260 return result ;
@@ -336,8 +350,11 @@ public String getTransactionUID(long transactionId){
336350 null , null , null , null );
337351
338352 String trxUID = null ;
339- if (cursor != null && cursor .moveToFirst ()){
340- trxUID = cursor .getString (cursor .getColumnIndexOrThrow (TransactionEntry .COLUMN_UID ));
353+ if (cursor != null ) {
354+ if (cursor .moveToFirst ()) {
355+ trxUID = cursor .getString (cursor .getColumnIndexOrThrow (TransactionEntry .COLUMN_UID ));
356+ }
357+ cursor .close ();
341358 }
342359
343360 return trxUID ;
@@ -355,9 +372,13 @@ public Cursor fetchAllRecords() {
355372
356373 @ Override
357374 public boolean deleteRecord (long rowId ) {
358- String transactionUID = getSplit (getUID (rowId )).getTransactionUID ();
375+ Split split = getSplit (rowId );
376+ String transactionUID = split == null ? null : split .getTransactionUID ();
359377 boolean result = deleteRecord (SplitEntry .TABLE_NAME , rowId );
360378
379+ if (!result ) //we didn't delete for whatever reason, invalid rowId etc
380+ return false ;
381+
361382 //if we just deleted the last split, then remove the transaction from db
362383 Cursor cursor = fetchSplitsForTransaction (transactionUID );
363384 if (cursor != null ){
@@ -376,6 +397,9 @@ public boolean deleteRecord(long rowId) {
376397 */
377398 public long getTransactionID (String transactionUID ){
378399 long id = -1 ;
400+ if (transactionUID == null )
401+ return id ;
402+
379403 Cursor c = mDb .query (TransactionEntry .TABLE_NAME ,
380404 new String []{TransactionEntry ._ID },
381405 TransactionEntry .COLUMN_UID + "=?" ,
0 commit comments