-8

I getting this error "Syntax error on token "}", delete this token." on the last line and I don't know why as I am new to Android.

   public void ok() throws UiObjectNotFoundException{
            //trying to find ok
                   try{
                       UiObject okDialog = new UiObject(new UiSelector().text("ok"));
                   if(okDialog.exists()){
                       System.out.println("Success");   
                       break;//if ok exists it should come out
                   }
                   //if ok not exists
                   if(!okDialog.exists()){
                         System.out.println("retrying");
                         getUiDevice().pressHome();
                         UiObject enterButton = new UiObject(new UiSelector().text("enter"));
                         enterButton.clickAndWaitForNewWindow();  
                   }
                   //validating again to check ok exists
                   if(okDialog.exists())
                         break;
                   else   //if ok not exists again printing the belwo message
                         System.out.println("unable to find"); 
                         getUiDevice().pressHome();
    //Here if ok not exists after going to home, i want to stop the whole script here..how to do this?
                   }catch (UiObjectNotFoundException e) {}

                   }
    } Error message appears here : Syntax error on token "}", delete this token
user3089474
  • 65
  • 3
  • 8

5 Answers5

1

If you have know just English knowledge, then you can eazly delete this } in shown your error message side

You can't add break in if condition, you need to return instead of break and remove {} in if conditions

} Error message appears here : Syntax error on token "}", delete this token 

Copy past this code instead of your method

  public void ok() throws  UiObjectNotFoundException
{
        //trying to find ok
               try{

                   UiObject okDialog = new UiObject(new UiSelector().text("ok"));

                   if(okDialog.exists())
                   System.out.println("Success");   
                   return;//if ok exists it should come out


               //if ok not exists
               if(!okDialog.exists())
                     System.out.println("retrying");
                     getUiDevice().pressHome();
                     UiObject enterButton = new UiObject(new UiSelector().text("enter"));
                     enterButton.clickAndWaitForNewWindow();  


               //validating again to check ok exists
               if(okDialog.exists())
                     return;
               else   //if ok not exists again printing the belwo message
                     System.out.println("unable to find"); 
                     getUiDevice().pressHome();
//Here if ok not exists after going to home, i want to stop the whole script here..how to do this?
               }
catch (UiObjectNotFoundExceptione) {}

               }
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
0

probably you are missing closing brackets (}) some where in your code. That's why you are getting this error. Have a close look at your code. and you will find solution.

InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
  • Are you sure he is missing ? – GrIsHu Dec 21 '13 at 06:26
  • @GrIsHu, Yes i am sure by looking at this code. because after catch block he just need to close only one bracket but he tries to close two brackets. You can have a look at his code. – InnocentKiller Dec 21 '13 at 06:59
  • That is what i said the last bracket is an extra that is why he need to delete it ,not to add any extra bracket. @Innocent Killer – GrIsHu Dec 21 '13 at 07:02
0

The last bracket is unnecessary so delete that bracket.

The error itself indicates to remove the last bracket }

  } Error message appears here : Syntax error on token "}", delete this token

If you get error "break cannot be used outside of a loop or a switch"

Then change your if condition as below:

      if(!okDialog.exists())
               System.out.println("unable to find"); 
                getUiDevice().pressHome();
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
0
}catch (UiObjectNotFoundException e){}
}

change to

}catch (UiObjectNotFoundException e){
}

notice that you closed right away the opening bracket after your catch

Rick Royd Aban
  • 904
  • 6
  • 33
0

Try this:

public void ok() throws UiObjectNotFoundException{
            //trying to find ok
                   try{
                       UiObject okDialog = new UiObject(new UiSelector().text("ok"));
                   if(okDialog.exists()){
                       System.out.println("Success");   
                       break;//if ok exists it should come out
                   }
                   //if ok not exists
                   if(!okDialog.exists()){
                         System.out.println("retrying");
                         getUiDevice().pressHome();
                         UiObject enterButton = new UiObject(new UiSelector().text("enter"));
                         enterButton.clickAndWaitForNewWindow();  
                   }
                   //validating again to check ok exists
                   if(okDialog.exists()){
                         //break;
                   }
                   else  {
                         //if ok not exists again printing the belwo message
                         System.out.println("unable to find"); 
                         getUiDevice().pressHome();
                   }
    //Here if ok not exists after going to home, i want to stop the whole script here..how to do this?
                   }catch (UiObjectNotFoundException e) {}    

    } 
Avijit
  • 3,834
  • 4
  • 33
  • 45
  • i tried out.."break cannot be used outside of a loop or a switch" error appears wherever the "break" is there in the code – user3089474 Dec 21 '13 at 06:30
  • see I have commented this break in my answer. Break cant be used in if condition Thats why you are getting this error. @user3089474 – Avijit Dec 21 '13 at 06:33