Hey,
Im new here, so sorry if i posted this in the wrong section!
I just found this bug for Kotlin while using the org.json library.
When JSON returns null, it gives a JSONObject$Null
While checking for null, == fails, and .equals warns to use ==
My code:
fun testNullJSON(jsonObject: JSONObject, valueKey: String){
val jsonArray = jsonObject.getJSONArray(valueKey)
for(i in 0 until jsonArray.length()) {
val jsonArrayValue = jsonArray[i] ?: continue // NOTE: First null check here
when (jsonArrayValue) {
is Int -> {
// My implementation ...
}
is String -> {
// My implementation ...
}
is JSONObject -> {
// My implementation ...
}
is JSONArray -> {
// My implementation ...
}
else -> {
// WARNING: IS ALWAYS FALSE
if (jsonArrayValue == null) {
println("jsonArrayValue == null")
} else {
println("jsonArrayValue != null")
println(jsonArrayValue) // PRINTS: null
}
// NOTE: - Checking with Binary results in wrong result!
@Suppress("ReplaceCallWithBinaryOperator")
if (jsonArrayValue.equals(null)) {
println("is a null value") // THIS GET ALSO PRINTED
} else {
println("Is not a null value")
println(jsonArrayValue) //Printed null while using ==
}
}
}
}
}
Result == and .equals null dont give the same result