When writing Kotlin code in Android Studio, you may encounter an error called “Type mismatch”.
This error occurs when the data type expected by Kotlin does not match the data type of the value that is actually passed.
There are various possible causes, including mixing up String and Int, differences between nullable and non-null types, mismatched function return types, and differences in the types of Lists and arrays.
This article introduces the main causes to check and basic solutions when “Type mismatch” appears in Kotlin in Android Studio.
- What Is “Type Mismatch”?
- Cause 1: Basic Types Such as String and Int Do Not Match
- Cause 2: Numeric Types Such as Int and Long or Float and Double Do Not Match
- Cause 3: Nullable and Non-Null Types Do Not Match
- Cause 4: The Function Argument Type Does Not Match
- Cause 5: The Function Return Type Does Not Match
- Cause 6: The Return Types of if or when Expressions Do Not Match
- Cause 7: The Element Types of List or MutableList Do Not Match
- Cause 8: The Array Type Does Not Match
- Cause 9: The Lambda Return Value Is Different from What Is Expected
- Cause 10: Required Android Types Such as View or Context Are Different
- Cause 11: The Type of a View Obtained with View Binding Is Different from What Is Expected
- Cause 12: Argument Types Do Not Match in Jetpack Compose
- When Type Inference Produces a Different Type Than Expected
- Check Required and Found in the Error Message
- Do Not Force Type Conversion
- Order of Checks When “Type Mismatch” Appears
- Check the Specific Error Location
- Summary
What Is “Type Mismatch”?
Kotlin’s “Type mismatch” is an error indicating that the type required in a certain location does not match the type of the value that is actually specified.
For example, suppose you have the following code.
val number: Int = "100"
In this code, the variable number is defined as an Int type.
However, the value "100" on the right-hand side is a string, which is a String type.
Therefore, Kotlin determines that a String type has been specified where an Int type is required and displays “Type mismatch”.
When “Type mismatch” is displayed, checking “Required” and “Found” or equivalent information in the error message makes it easier to identify the required type and the actual type.
Cause 1: Basic Types Such as String and Int Do Not Match
One common cause is mixing up basic types such as String, Int, Double, and Boolean.
val age: Int = "20"
In this case, age requires an Int type, but the string "20" is specified.
If you want to treat it as a number, write it as an Int type as follows.
val age: Int = 20
To convert a String value to an Int, use toInt() or a similar function.
val text = "20"
val age: Int = text.toInt()
Calling toInt() on a string that cannot be converted to a number causes an exception. If the input value is uncertain, you can also use toIntOrNull().
Cause 2: Numeric Types Such as Int and Long or Float and Double Do Not Match
In Kotlin, even numeric types are not always automatically converted to another numeric type.
val number: Long = 100
Numeric literals may be handled appropriately depending on the situation, but a variable that has already been defined as an Int type cannot be directly assigned to a Long type.
val intValue: Int = 100
val longValue: Long = intValue
In this case, use toLong() to convert the type.
val intValue: Int = 100
val longValue: Long = intValue.toLong()
Similarly, use conversions such as toDouble(), toFloat(), and toInt() depending on the required type.
Cause 3: Nullable and Non-Null Types Do Not Match
In Kotlin, types that allow null and types that do not allow null are clearly distinguished.
For example, String and String? are treated as different types.
val text: String? = null
val message: String = text
text is String? and may be null.
On the other hand, message is a String type that does not allow null.
Therefore, assigning it directly results in a type mismatch.
If you want to use another value when it is null, you can use the Elvis operator.
val text: String? = null
val message: String = text ?: "Default"
You can also process the value after confirming that it is not null.
val text: String? = "Hello"
if (text != null) {
val message: String = text
}
Using !! allows a nullable type to be forcibly treated as non-null, but an exception occurs if it is actually null. As a general rule, it is more appropriate to handle it safely using a null check or the Elvis operator.
Cause 4: The Function Argument Type Does Not Match
“Type mismatch” also occurs when the type of the value passed to a function differs from the argument type specified by the function.
fun showNumber(number: Int) {
println(number)
}
showNumber("100")
showNumber() requires an Int argument, but an error occurs because the String "100" is passed.
Correctly, pass an Int value.
showNumber(100)
When converting from a String type, perform type conversion as needed.
Cause 5: The Function Return Type Does Not Match
“Type mismatch” also occurs when the return type specified for a function differs from the type of the value actually returned.
fun getNumber(): Int {
return "100"
}
This function specifies Int as the return type, but actually returns a String type.
To return an Int type, write it as follows.
fun getNumber(): Int {
return 100
}
Conversely, if you want to return a string, change the function return type to String.
Cause 6: The Return Types of if or when Expressions Do Not Match
In Kotlin, if and when can be used as expressions.
Therefore, depending on the types of values returned from each branch, the result may be a different type from what you expected.
val result: Int = if (true) {
100
} else {
"error"
}
One branch returns an Int type, while the other returns a String type, so result cannot be treated as an Int type.
Modify the branches so that they return the same type.
val result: Int = if (true) {
100
} else {
0
}
Cause 7: The Element Types of List or MutableList Do Not Match
With List and MutableList, the type of the stored elements is also important.
val numbers: List<Int> = listOf("1", "2", "3")
The left side specifies List<Int>, but the right side contains String values.
To make it a List of Int values, write it as follows.
val numbers: List<Int> = listOf(1, 2, 3)
If you want to treat it as a List of String values, change the type declaration to List<String>.
Cause 8: The Array Type Does Not Match
In Kotlin, Array<Int> and IntArray are also treated as different types.
If the API you are using requires a specific array type, passing another type of array may result in a type mismatch.
val array: IntArray = intArrayOf(1, 2, 3)
If an error is displayed, check the array type required by the function or API and the type of the array you are actually creating.
Cause 9: The Lambda Return Value Is Different from What Is Expected
Lambda expressions are frequently used in Kotlin, and “Type mismatch” may also occur when the type of the value returned by a lambda differs from the expected type.
val action: () -> Int = {
"Hello"
}
This lambda expression must return an Int type, but it actually returns a String type.
Modify it so that it returns an Int type.
val action: () -> Int = {
100
}
Cause 10: Required Android Types Such as View or Context Are Different
In Android development, there are many situations where Android-specific types such as Context, Activity, Fragment, and View are passed as arguments.
If you pass an object of a different type from the one required by the API, “Type mismatch” may be displayed.
For example, this can happen when a different kind of object is passed where a Context is required.
If an error is displayed, check the argument definitions of the constructor or function being called and confirm which type is required.
Cause 11: The Type of a View Obtained with View Binding Is Different from What Is Expected
When using View Binding, the type of the generated property is determined by the type of View defined in the XML.
For example, if a View is defined as a TextView in XML but you try to treat it as a Button type, the types do not match.
If a Type mismatch occurs in relation to View Binding, check not only the Kotlin code, but also which View is defined in the corresponding XML layout.
Cause 12: Argument Types Do Not Match in Jetpack Compose
In Jetpack Compose as well, “Type mismatch” may occur because the type of an argument passed to a Composable function is different.
For example, this can happen when an Int type is passed directly to an argument that requires a String type.
val count = 10
Text(text = count)
Because the text argument of Text() requires a string, convert it to a String type as needed.
val count = 10
Text(text = count.toString())
When Type Inference Produces a Different Type Than Expected
In Kotlin, even if you do not explicitly write a type, the type is automatically inferred from the value.
val value = 100
In this case, value is normally inferred as an Int type.
If you later try to treat it as another type, it may result in a Type mismatch.
If the type is difficult to understand, temporarily adding an explicit type declaration can make it easier to identify the problem.
val value: Int = 100
Check Required and Found in the Error Message
When investigating the cause of “Type mismatch”, check the error message displayed in Android Studio.
The error message may display the required type and the type that was actually specified.
Type mismatch.
Required: Int
Found: String
In this case, you can see that a String type is being passed where an Int type is required.
Check which type should be used, and either change the variable type or convert the value to the appropriate type.
Do Not Force Type Conversion
When a Type mismatch occurs, you may be tempted to simply add a type cast to resolve it.
val value = data as String
However, forcibly casting a value that is not actually a String type may cause an exception at runtime.
If you need to change the type, first check what type the original value is and why it has that type.
If necessary, you can also use the safe cast as?.
val value = data as? String
Type mismatch means that the compiler has detected a type inconsistency. Rather than forcing the error to disappear, it is important to determine which type is actually correct and fix the code accordingly.
Order of Checks When “Type Mismatch” Appears
If you do not know the cause, checking the following items in order can make it easier to isolate the problem.
- Check the required type and the actual type in the error message.
- Check the type specified for the variable.
- Check the type of the value being assigned.
- Check the types of function arguments and return values.
- Check the difference between nullable and non-null types.
- Check the differences between numeric types such as Int, Long, Float, and Double.
- Check the element types of List, Array, and similar types.
- Perform the appropriate type conversion if necessary.
- Check the type required by the Android API or library.
- Check whether the issue is being forcibly resolved with a cast.
Check the Specific Error Location
When resolving “Type mismatch”, it is important not only to look at the error name, but also to check which value is being treated as which type.
Example:
Required: Int
Found: String
→ Check whether String needs to be converted to Int
Required: String
Found: Int
→ Check whether toString() is needed
Required: String
Found: String?
→ Check whether the type allows null
Required: Long
Found: Int
→ Check whether toLong() is needed
List<Int> is required
List<String> is being passed
→ Check the element type inside the List
Summary
“Type mismatch” in Kotlin in Android Studio is an error that occurs when the required data type does not match the data type of the value that is actually specified.
Particularly common causes include differences between basic types such as String and Int, differences between numeric types, differences between nullable and non-null types, and mismatches in function argument and return types.
When the error is displayed, first check the Android Studio error message to identify the required type and the actual type.
Then determine which type should actually be used and either modify the type of the variable or function, or perform the appropriate type conversion using toInt(), toLong(), toString(), or similar functions.
Also, with nullable types, collections, Android APIs, and Jetpack Compose, Type mismatch can occur because of differences in types other than simple basic types, so it is important to isolate the cause while checking the type required at the error location.
