Error “must not be null” in Kotlin

二次信任 提交于 2019-12-04 02:52:56

问题


There are multiple files in a .zip file, which I'm trying to get. Trying to unzip the files provides a java.lang.IllegalStateException: zis.nextEntry must not be null. How to do it the right way?

@Throws(IOException::class)
    fun unzip(zipFile: File, targetDirectory: File) {
        val zis = ZipInputStream(
                BufferedInputStream(FileInputStream(zipFile)))
        try {
            var ze: ZipEntry
            var count: Int
            val buffer = ByteArray(8192)
            ze = zis.nextEntry
            while (ze != null) {
                val file = File(targetDirectory, ze.name)
                val dir = if (ze.isDirectory) file else file.parentFile
                if (!dir.isDirectory && !dir.mkdirs())
                    throw FileNotFoundException("Failed to ensure directory: " + dir.absolutePath)
                if (ze.isDirectory)
                    continue
                val fout = FileOutputStream(file)
                try {
                    count = zis.read(buffer)
                    while (count != -1) {
                        fout.write(buffer, 0, count)
                        count = zis.read(buffer)
                    }
                } finally {
                    fout.close()
                    zis.closeEntry()
                    ze = zis.nextEntry
                }
            }
        } finally {
            zis.closeEntry()
            zis.close()
        }
    }

回答1:


The ZipEntry you read from the stream will be null when you reach the end of the file, so you have to make the variable that you store it in nullable:

var ze: ZipEntry?

You were allowed to assign the values you read to a non-nullable variable because they had the platform type ZipEntry!, since it's a Java API - in this case you have to determine whether it can be null. See the docs about platform types for more information.




回答2:


You define the variable ze like this var ze: ZipEntry. So the type is ZipEntry and not ZipEntry? (nullable type).

If you change var ze: ZipEntry by var ze: ZipEntry?, the variable can be null.

You can check the doc for Null Safety. It's one of the big thing with Kotlin.




回答3:


I had a similar issue when trying to build this functionality that lead me here. Once I made zipEntry nullable an error popped up in an if statement which said zipEntry? is a mismatch to zipEntry, I was able to solve that by using zipEntry!! guaranteeing it was not null.

      while(zippedFile != null) {
            fileName = zippedFile.name //It wasn't able to smart cast once zipEntry was nullable
            if (zippedFile.isDirectory) {} //Here it had a type mismatch

This was the solution I was able to work out.

  while(zippedFile != null) {
            fileName = zippedFile!!.name //Adding !! (not null) allowed it to safely smart cast
            if (zippedFile!!.isDirectory) {} //Here adding not null removed the type mismatch

If anyone working in Kotlin runs into this issue I hope it can help!



来源:https://stackoverflow.com/questions/46661956/error-must-not-be-null-in-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!