Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "must not be null" in Kotlin

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()
        }
    }
like image 492
Petras Bartusis Avatar asked Oct 10 '17 08:10

Petras Bartusis


People also ask

Why can’t I use null in Kotlin?

Other issues caused by external Java code. In Kotlin, the type system distinguishes between references that can hold null (nullable references) and those that cannot (non-null references). For example, a regular variable of type String cannot hold null: To allow nulls, you can declare a variable as a nullable string by writing String?:

What is the default value of string in Kotlin?

Value of string is : null In this Kotlin Tutorial, we have learnt how to handle the compilation error caused during assignment of null value to a non-null declared String variable.

Why are data types in Kotlin non-nullable?

In Kotlin by default, the data types are of non-nullable types i.e trying to assign a value as null would cause an error. This is done to eliminate the NullPointerException error in the code which is common in other programming languages like Java.

What is the type system in Kotlin?

Kotlin's type system is aimed at eliminating the danger of null references, also known as The Billion Dollar Mistake. One of the most common pitfalls in many programming languages, including Java, is that accessing a member of a null reference will result in a null reference exception.


1 Answers

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.

like image 163
zsmb13 Avatar answered Sep 29 '22 09:09

zsmb13