Nicola
04/06/2023, 12:43 PMVampire
04/06/2023, 12:49 PMnull
when the NPE happened to easily identify the code where it happened in that line. But I have no idea whether that also works with Kotlin.Nicola
04/06/2023, 12:51 PMNicola
04/06/2023, 12:52 PMclass RenderService(private val outputPath: String) {
var loadingDuration: Duration = Duration.ZERO
var renderingDuration: Duration = Duration.ZERO
var savingDuration: Duration = Duration.ZERO
private val mavTemplateBytes: ByteArray
init {
val template: InputStream = getFileContentFromResources("templates/MAV.pdf")
?: throw RuntimeException("Failed to load template")
mavTemplateBytes = template.readAllBytes()
}
Vampire
04/06/2023, 12:54 PMNicola
04/06/2023, 12:54 PMNicola
04/06/2023, 12:54 PMNicola
04/06/2023, 1:00 PMNicola
04/06/2023, 1:01 PMNicola
04/06/2023, 1:05 PMVampire
04/06/2023, 1:05 PMVampire
04/06/2023, 1:05 PMNicola
04/06/2023, 1:06 PMVampire
04/06/2023, 1:06 PMNicola
04/06/2023, 1:07 PMVampire
04/06/2023, 1:08 PMNicola
04/06/2023, 1:10 PMChris Lee
04/06/2023, 2:59 PMval template: InputStream = getFileContentFromResources("templates/MAV.pdf")
?: throw RuntimeException("Failed to load
By defining the type as InputStream
(non-null) youâre coercing a null value into that from getFileContentFromResource. Make that nullable (InputStream?) and let the smart cast handle subsequent uses.Nicola
04/06/2023, 3:00 PMVampire
04/06/2023, 3:02 PMChris Lee
04/06/2023, 3:03 PMNicola
04/06/2023, 3:04 PMprivate fun getFileContentFromResources(name: String) =
RenderService::class.java.classLoader.getResourceAsStream(name)
Nicola
04/06/2023, 3:04 PMNicola
04/06/2023, 3:04 PMNicola
04/06/2023, 3:05 PMNicola
04/06/2023, 3:05 PMclass RenderService(private val outputPath: String) {
var loadingDuration: Duration = Duration.ZERO
var renderingDuration: Duration = Duration.ZERO
var savingDuration: Duration = Duration.ZERO
private val mavTemplateBytes: ByteArray
init {
val template: InputStream = getFileContentFromResources("templates/MAV.pdf")
?: throw RuntimeException("Failed to load template")
mavTemplateBytes = template.readAllBytes()
}
Nicola
04/06/2023, 3:05 PMNicola
04/06/2023, 3:06 PMChris Lee
04/06/2023, 3:08 PMNicola
04/06/2023, 3:10 PMNicola
04/06/2023, 3:10 PMChris Lee
04/06/2023, 3:12 PMNicola
04/06/2023, 3:13 PMNicola
04/06/2023, 3:14 PMChris Lee
04/06/2023, 3:16 PM!
is non-nullable, ?
is nullable. No need to define types on those vals, they are inferred.Chris Lee
04/06/2023, 3:16 PMtest("foo") {
val template = getFileContentFromResources("whatever")
val template2 = getFileContentFromResources("whatever") ?: error("whoops")
val foo = template2.readAllBytes()
}
Vampire
04/06/2023, 3:17 PM!
is not non-nullableVampire
04/06/2023, 3:17 PMVampire
04/06/2023, 3:18 PMNicola
04/06/2023, 3:19 PMVampire
04/06/2023, 3:19 PMVampire
04/06/2023, 3:19 PMNicola
04/06/2023, 3:19 PMNicola
04/06/2023, 3:19 PMChris Lee
04/06/2023, 3:19 PMI donât see it in my ide, is it ultimate feature?Not sure. Itâs local variable type hints.
Vampire
04/06/2023, 3:20 PMNicola
04/06/2023, 3:20 PMVampire
04/06/2023, 3:20 PMNicola
04/06/2023, 3:20 PMChris Lee
04/06/2023, 3:21 PMNicola
04/06/2023, 3:23 PMNicola
04/06/2023, 3:23 PMVampire
04/06/2023, 3:24 PMNicola
04/06/2023, 3:24 PMChris Lee
04/06/2023, 3:24 PMChris Lee
04/06/2023, 3:26 PMNicola
04/06/2023, 3:26 PMNicola
04/06/2023, 3:26 PM