When writing code in Kotlin, you may encounter an error called “Expecting ‘)'”.
This error occurs when the Kotlin compiler is analyzing code and cannot find a closing parenthesis ) even though one is required.
Possible causes include forgetting to close parentheses () used in function calls, conditional expressions, function definitions, and other constructs, as well as syntax mismatches caused by mistakes in arguments, operators, quotation marks, and similar elements.
This article introduces the main causes of the “Expecting ‘)'” error in Kotlin and the basic ways to resolve it.
- What Is “Expecting ‘)’ “?
- Cause 1: The Closing Parenthesis of a Function Call Is Missing
- Cause 2: The Parenthesis in the Condition of an if Statement Is Not Closed
- Cause 3: The Parenthesis Around the Subject of a when Expression Is Not Closed
- Cause 4: The Parameter List in a Function Definition Is Not Closed
- Cause 5: Parentheses Are Mismatched in Multiple Function Calls
- Cause 6: There Is a Syntax Error in the Middle of the Arguments
- Cause 7: There Is an Unnecessary Comma
- Cause 8: There Is No Expression on the Right Side of an Operator
- Cause 9: The Closing Quotation Mark of a String Is Missing
- Cause 10: The Code Is Cut Off Because of the Position of a Comment
- Cause 11: The Problem Is Before the Location Where the Error Is Displayed
- Check the Number and Matching of Parentheses
- Split Complex Expressions to Check Them
- Check Matching Parentheses in the IDE
- Points to Check When “Expecting ‘)'” Appears
- Summary
What Is “Expecting ‘)’ “?
“Expecting ‘)'” is a syntax error indicating that the compiler has determined that a closing parenthesis ) is required by that point while analyzing the code.
For example, it may occur if you forget to write the closing parenthesis of a function call.
println("Hello"
In this code, there is no ) to close the println() call.
Add the closing parenthesis.
println("Hello")
Cause 1: The Closing Parenthesis of a Function Call Is Missing
One of the most basic causes is forgetting to write the ) that closes a function call.
printMessage("Hello"
In this case, there is no ) corresponding to the ( that begins the function call.
Add a closing parenthesis at the end of the function call.
printMessage("Hello")
Cause 2: The Parenthesis in the Condition of an if Statement Is Not Closed
“Expecting ‘)'” may also be displayed when the parentheses surrounding the condition of an if statement are not closed.
if (age >= 18 {
println("Adult")
}
In this example, there is no ) to close the conditional expression age >= 18.
Add a closing parenthesis at the end of the conditional expression.
if (age >= 18) {
println("Adult")
}
Cause 3: The Parenthesis Around the Subject of a when Expression Is Not Closed
When the target value of a when expression is enclosed in parentheses, a syntax error occurs if the closing parenthesis is missing.
when (value {
1 -> println("One")
2 -> println("Two")
}
In this code, there is no closing parenthesis after value.
Close it correctly.
when (value) {
1 -> println("One")
2 -> println("Two")
}
Cause 4: The Parameter List in a Function Definition Is Not Closed
Forgetting to close the parentheses () used when defining a function may also cause “Expecting ‘)'” to be displayed.
fun add(a: Int, b: Int {
return a + b
}
In this example, the parentheses surrounding the parameters a and b are not closed.
Add the closing parenthesis.
fun add(a: Int, b: Int) {
return a + b
}
Cause 5: Parentheses Are Mismatched in Multiple Function Calls
In code where one function is called inside another function, multiple parentheses are nested, making it easier to forget a closing parenthesis.
println(calculate(10, 20)
In this code, there is a closing parenthesis for calculate(), but the closing parenthesis for println() is missing.
Check whether each opening parenthesis has a corresponding closing parenthesis.
println(calculate(10, 20))
Cause 6: There Is a Syntax Error in the Middle of the Arguments
Even if the closing parenthesis itself exists, a syntax error in the argument section of a function may prevent the compiler from recognizing the parentheses correctly, causing “Expecting ‘)'” to be displayed.
showMessage("Hello", name = )
In this example, the required value after name = is missing.
Specify the required value.
showMessage("Hello", name = "Taro")
Even if the error is displayed near the closing parenthesis, the actual cause may be the argument declaration before it.
Cause 7: There Is an Unnecessary Comma
If there is an unnecessary comma in the middle of a function call or parameter definition, the compiler may expect another element and fail to parse the syntax.
showMessage("Hello", , "World")
In this case, there is no argument between the two commas.
Remove the unnecessary comma.
showMessage("Hello", "World")
Cause 8: There Is No Expression on the Right Side of an Operator
If a conditional expression or calculation inside parentheses ends partway through, the syntax may become invalid and “Expecting ‘)'” may be displayed.
if (age >= ) {
println("Adult")
}
In this example, there is no value on the right side of the comparison operator >=.
Add the required value.
if (age >= 18) {
println("Adult")
}
Cause 9: The Closing Quotation Mark of a String Is Missing
Even if the parenthesis itself exists, if a string before it does not have a closing quotation mark, the compiler may be unable to recognize the following ) correctly.
println("Hello)
In this example, the closing parenthesis exists, but the quotation mark " that closes the string is missing.
Close the quotation mark correctly.
println("Hello")
Even when “Expecting ‘)'” is displayed, the closing parenthesis itself is not necessarily missing. A preceding syntax error involving arguments, operators, quotation marks, or similar elements may prevent the parenthesis from being recognized correctly.
Cause 10: The Code Is Cut Off Because of the Position of a Comment
When a comment is added, part of the required code may be treated as a comment, causing the parentheses to become mismatched.
println(
"Hello"
// )
If a required closing parenthesis is included in a comment like this, the compiler sees the code as if the ) does not exist.
Check the range of the comment and make sure that a required parenthesis has not been commented out.
Cause 11: The Problem Is Before the Location Where the Error Is Displayed
“Expecting ‘)'” may be displayed on a later line or at the location of another construct rather than at the actual place where the parenthesis was omitted.
Because the compiler analyzes code in order, if an opening parenthesis remains unclosed as it proceeds to later code, it may display an error at another location because it expects a closing parenthesis there.
Therefore, you need to check not only the line where the error is displayed but also the preceding function calls, conditional expressions, function definitions, and other code.
Check the Number and Matching of Parentheses
When “Expecting ‘)'” is displayed, first check whether ( and ) are correctly matched in the code.
If there are fewer closing parentheses than opening parentheses, identify the missing location and add the required parenthesis.
However, even if the number is the same, the syntax may not be parsed correctly if a parenthesis is closed in the wrong position.
It is important to check not only the number of parentheses but also “which expression or function call each parenthesis closes.”
Split Complex Expressions to Check Them
If multiple function calls or conditions are included in a single expression, temporarily splitting the code can make it easier to identify the cause.
val result = calculate(10, 20)
println(result)
By dividing code with many parentheses on a single line into multiple operations, it becomes easier to check which parentheses are mismatched.
Check Matching Parentheses in the IDE
In IDEs such as Android Studio, when you place the cursor on ( or ), the corresponding parenthesis may be highlighted.
When function calls are nested multiple levels deep, using this feature can make it easier to find a missing closing parenthesis.
Points to Check When “Expecting ‘)'” Appears
When “Expecting ‘)'” appears, focus on checking whether every opening parenthesis is correctly closed.
- Are the
()of function calls closed correctly? - Are the parentheses of
ifandwhenclosed? - Are the
()around the parameter section of function definitions closed? - Are the number and positions of parentheses in nested function calls correct?
- Is a value or expression missing in the middle of the arguments?
- Is there an unnecessary comma remaining?
- Is the required expression present on the right side of an operator?
- Did you forget to close a string quotation mark?
- Have you commented out a required
)? - Is there a syntax error in the code before the error line?
In particular, if the error is displayed far from the parenthesis where the actual problem occurred, an opening ( somewhere earlier may not have been closed.
Summary
Kotlin’s “Expecting ‘)'” is a syntax error that is displayed when the compiler analyzes code and cannot find a closing parenthesis ) even though one is required.
Typical causes include forgetting to close parentheses used in function calls, if or when conditions, and function definitions, as well as mismatched parentheses in nested function calls.
In addition, “Expecting ‘)'” may also be displayed because of syntax errors other than parentheses, such as missing arguments, incorrect operators, or unclosed quotation marks in strings.
Basically, the error can be resolved by checking the correspondence between ( and ) and confirming that the syntax of arguments and conditional expressions is also written correctly.
When “Expecting ‘)'” appears, rather than looking only at the error location, checking the preceding code while asking “where is the opening parenthesis that has not yet been closed?” makes it easier to identify the cause.
