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 brace } even though one is required.
Possible causes include forgetting to close braces {} used in functions, classes, if, when, loops, lambda expressions, and other constructs, as well as mismatched braces caused by a preceding syntax error.
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 Brace of a Function Is Missing
- Cause 2: The Brace of an if Statement Is Not Closed
- Cause 3: The Closing Brace of a when Expression Is Missing
- Cause 4: The Brace of a Loop Such as for or while Is Not Closed
- Cause 5: The Closing Brace of a Class Is Missing
- Cause 6: The Brace of a Lambda Expression Is Not Closed
- Cause 7: A Brace Is Missing in a Nested Block
- Cause 8: A Closing Brace Has Been Removed by Commenting Out Code
- Cause 9: An Unclosed String or Comment Is Affecting the Code
- Cause 10: The Problem Is Before the Location Where the Error Is Displayed
- Check the Number and Matching of Braces
- Check by Adjusting the Indentation
- Check Matching Braces in the IDE
- Temporarily Simplify the Code to Identify the Cause
- Points to Check When “Expecting ‘}'” Appears
- Summary
What Is “Expecting ‘}'”?
“Expecting ‘}'” is a syntax error indicating that the compiler has determined that a closing brace } is required by that point while analyzing the code.
For example, it may occur if you forget to write the brace that closes a function block.
fun main() {
println("Hello")
In this code, there is no } to close the main() block.
Add the closing brace.
fun main() {
println("Hello")
}
Cause 1: The Closing Brace of a Function Is Missing
One of the most basic causes is forgetting to write the } that closes a function’s processing block.
fun showMessage() {
println("Hello")
In this case, there is no } corresponding to the { that marks the beginning of the function.
Add a closing brace at the end of the function.
fun showMessage() {
println("Hello")
}
Cause 2: The Brace of an if Statement Is Not Closed
“Expecting ‘}'” may also be displayed when a closing brace for an if statement block is missing.
fun main() {
val age = 20
if (age >= 18) {
println("Adult")
}
In this example, the braces required for the if block and the main() block are not properly matched.
Close each block correctly.
fun main() {
val age = 20
if (age >= 18) {
println("Adult")
}
}
Cause 3: The Closing Brace of a when Expression Is Missing
A syntax error also occurs in a when expression if the } that closes the block is missing.
when (value) {
1 -> println("One")
2 -> println("Two")
In this code, there is no closing brace for the when expression.
Add } at the end.
when (value) {
1 -> println("One")
2 -> println("Two")
}
Cause 4: The Brace of a Loop Such as for or while Is Not Closed
In loops such as for and while, a block that has been opened must also be closed.
for (i in 1..5) {
println(i)
In this case, the } that closes the for block is missing.
for (i in 1..5) {
println(i)
}
In particular, when a loop contains an if statement or another loop, it may become difficult to determine which brace corresponds to which block.
Cause 5: The Closing Brace of a Class Is Missing
A class definition also requires a brace to close the class body.
class User {
fun showName() {
println("Taro")
}
In this code, the showName() block is closed, but there is no } to close the User class itself.
Add a closing brace at the end of the class.
class User {
fun showName() {
println("Taro")
}
}
Cause 6: The Brace of a Lambda Expression Is Not Closed
Because lambda expressions also use {}, forgetting a closing brace may cause “Expecting ‘}'” to be displayed.
val action = { value: Int ->
println(value)
In this case, there is no } to close the processing block of the lambda expression.
val action = { value: Int ->
println(value)
}
Cause 7: A Brace Is Missing in a Nested Block
When multiple blocks are nested, such as an if statement inside a function and a loop inside that if statement, closing braces are more likely to be omitted.
fun main() {
if (true) {
for (i in 1..3) {
println(i)
}
}
In code like this, the number and positions of the braces can become difficult to follow, making it easy to forget to close one of the blocks.
Adjust the indentation and check whether each { has a corresponding }.
fun main() {
if (true) {
for (i in 1..3) {
println(i)
}
}
}
Cause 8: A Closing Brace Has Been Removed by Commenting Out Code
When temporarily commenting out code, you may accidentally include a required closing brace in the comment.
fun main() {
println("Hello")
/*
println("Test")
}
If the comment range is incorrect like this, required code or braces may appear to be missing from the compiler’s perspective.
Check the beginning and end of the block comment /* */ and make sure that required braces have not been commented out.
Cause 9: An Unclosed String or Comment Is Affecting the Code
Even if the brace actually exists, an unclosed string or comment before it may prevent the compiler from correctly analyzing the code that follows, causing “Expecting ‘}'” to be displayed.
fun main() {
val message = "Hello
println(message)
}
In this example, the } itself exists, but the closing quotation mark for the string is missing.
Close the quotation mark correctly.
fun main() {
val message = "Hello"
println(message)
}
Even when “Expecting ‘}'” is displayed, the closing brace itself is not necessarily missing. A preceding syntax error, such as an unclosed quotation mark or comment, may prevent the brace from being recognized correctly.
Cause 10: The Problem Is Before the Location Where the Error Is Displayed
“Expecting ‘}'” may be displayed at the end of the file or on another line rather than at the actual location where the brace was omitted.
Because the compiler analyzes code from top to bottom, if an opened block remains unclosed until the end, it may display an error near the end of the file because it expects a closing brace.
Therefore, you need to check not only the line where the error is displayed but also earlier functions, classes, conditional branches, loops, and other blocks.
Check the Number and Matching of Braces
When “Expecting ‘}'” is displayed, first check whether { and } are correctly matched in the code.
If there are simply fewer closing braces than opening braces, identify the missing location and add the required brace.
However, even if the number of braces is the same, the structure of the code may be broken if a brace is closed in the wrong position.
It is important to check not only the number of braces but also “which block each brace closes.”
Check by Adjusting the Indentation
If you can no longer determine how the braces correspond to each other, adjusting the indentation of the code can make it easier to identify the cause.
fun main() {
if (true) {
println("Hello")
}
}
By aligning the indentation, it becomes easier to visually check the relationship between each opening brace and closing brace.
IDEs such as Android Studio provide automatic code formatting, which can also help when checking where the code structure has become broken.
Check Matching Braces in the IDE
In IDEs such as Android Studio, when you place the cursor on { or }, the corresponding brace may be highlighted.
In long functions or deeply nested code, using this feature to check which braces correspond to each other makes it easier to find a missing closing brace.
Temporarily Simplify the Code to Identify the Cause
If you cannot identify the cause in a long function or complex class, temporarily deleting or commenting out parts that may contain the problem can make the code easier to check.
However, when commenting out code, be careful not to include required braces in the comment.
By checking each block individually, it becomes easier to identify which part causes the brace matching to break when it is added.
Points to Check When “Expecting ‘}'” Appears
When “Expecting ‘}'” appears, focus on checking whether every opened block is correctly closed.
- Are the
{}of functions closed correctly? - Are the
{}of classes closed correctly? - Are the
{}ofifandwhenclosed? - Are loops such as
forandwhileclosed? - Did you forget the closing brace of a lambda expression?
- Are the number and positions of braces in nested blocks correct?
- Have you commented out a required
}? - Did you forget to close a quotation mark or comment?
- Is there a syntax error in the code before the error line?
- Are all blocks closed by the end of the file?
In particular, if the error is displayed at the end of the file, a { opened 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 brace } even though one is required.
Typical causes include missing closing braces in functions, classes, if, when, loops, and lambda expressions, as well as mismatched braces in nested blocks.
In addition, “Expecting ‘}'” may also be displayed because of syntax errors other than braces, such as unclosed quotation marks in strings or unclosed comments.
Basically, the error can be resolved by checking the correspondence between { and } and making sure that each block is properly closed while adjusting the indentation.
When “Expecting ‘}'” appears, rather than looking only at the error location, checking the preceding code while asking “where is the block that was opened but has not yet been closed?” makes it easier to identify the cause.
