-1

In the code below db.setTransactionSuccessful(); gives error Unreachable code. Can anyone tell me how to solve this problem please?

public boolean updateDiaryEntry(String title, long rowId)
        {

            ContentValues newValue = new ContentValues();
            newValue.put(Constants.TITLE_NAME, title);

            db.beginTransaction();


            return db.update(Constants.TABLE_NAME , newValue , Constants.KEY_ID + "= ?" , 
                new String[]{ Double.valueOf(rowId).toString() })>0;

            db.setTransactionSuccessful();
            db.endTransaction();    

        }
shuttle87
  • 15,466
  • 11
  • 77
  • 106
  • There's 2 lines of code after you `return` from the function, these lines will never be executed because you have already left the function. This is why you get the unreachable code message. – shuttle87 Jun 06 '14 at 16:13

2 Answers2

2

You are doing a return the line before that one which will exit the function.

Scott Mildenberger
  • 1,456
  • 8
  • 17
1

There's 2 lines of code after you return from the function, these lines will never be executed because you have already left the function. This is why you get the unreachable code message. You don't want to have lines of code after a return statement:

return db.update(Constants.TABLE_NAME , newValue , Constants.KEY_ID + "= ?" , 
            new String[]{ Double.valueOf(rowId).toString() })>0;   //returned from function on this line

db.setTransactionSuccessful(); //therefore you never get to this line
db.endTransaction();  

Instead you probably want to do something like this:

db_result = db.update(Constants.TABLE_NAME , newValue , Constants.KEY_ID + "= ?" , 
            new String[]{ Double.valueOf(rowId).toString() })>0;

if(db_result){
     db.setTransactionSuccessful(); //Now you can do this conditional on the result of the update
}
db.endTransaction();
return db_result;

By creating a variable to store the result of updating the database you can do the db related cleanup/close functions before you return from the function.

shuttle87
  • 15,466
  • 11
  • 77
  • 106