java Kotlin 中的错误“不能为空”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46661956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error "must not be null" in Kotlin
提问by Petras Bartusis
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?
.zip 文件中有多个文件,我正在尝试获取这些文件。尝试解压缩文件会提供 java.lang.IllegalStateException:zis.nextEntry 不能为 null。如何以正确的方式做到这一点?
@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()
}
}
回答by zsmb13
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:
在ZipEntry
你从流中读取会null
当你达到了文件的末尾,所以你必须让你将其存储在可空的变量:
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 docsabout platform types for more information.
您可以将读取的值分配给不可为空的变量,因为它们具有平台类型ZipEntry!
,因为它是 Java API - 在这种情况下,您必须确定它是否可以是null
. 有关更多信息,请参阅有关平台类型的文档。
回答by Kevin Robatel
You define the variable ze
like this var ze: ZipEntry
. So the type is ZipEntry
and not ZipEntry?
(nullable type).
您可以ze
像这样定义变量var ze: ZipEntry
。所以类型是ZipEntry
和不是ZipEntry?
(可为空类型)。
If you change var ze: ZipEntry
by var ze: ZipEntry?
, the variable can be null.
如果您更改var ze: ZipEntry
by var ze: ZipEntry?
,则该变量可以为空。
You can check the doc for Null Safety. It's one of the big thing with Kotlin.
您可以查看Null Safety的文档。这是 Kotlin 的一件大事。
回答by HamBeast
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.
在尝试构建引导我来到这里的功能时,我遇到了类似的问题。一旦我将 zipEntry 设为可空,在 if 语句中会弹出一个错误,该语句表示 zipEntry?与 zipEntry 不匹配,我能够通过使用 zipEntry 来解决这个问题!!保证它不为空。
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!
如果任何在 Kotlin 工作的人遇到这个问题,我希望它能有所帮助!