问题标签 [kotlin-null-safety]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
776 浏览

kotlin - Kotlin 地图地图中的括号表示法

在 Kotlin 中,可以将括号表示法与 Map 一起使用,因此以下代码:

印刷:

那太棒了。但是为什么我不能执行以下操作

它会导致编译错误:在 Map 类型的可为空的接收器上只允许安全 (?.) 或非空断言 (!!.) 调用?

处理这个问题的正确方法是什么?

0 投票
1 回答
518 浏览

kotlin - 如何处理来自 Kotlin 的 find 函数的 null 返回?

我有一个对象列表,例如:

接下来,我想通过name条件从这个列表中找到一个对象,并获取找到的对象的一个id​​字段的值。所以,我find在列表中使用函数调用:

但是这个不能编译在可以为空的接收器上只允许安全或非调用调用。那么,我应该如何处理可能的null回报find?我尝试使用 Elvis 运算符返回一个空字符串,否则,例如:

但这仍然无法编译。

0 投票
4 回答
1429 浏览

android - 有什么区别!!和 ?在 kotlin 的零安全性中?

在下面的例子中,我对?and的用法有点困惑。!!

我应该使用哪个空安全运算符?

0 投票
1 回答
65 浏览

kotlin - Kotlin 中 IF 比较的空安全性

我有一个关于 Kotlin 如何管理NULL安全比较的问题。我有这个代码:

它工作正常,但是如果我将其更改为

它抛出 a NullPointerException,这很明显,因为应用程序启动时oldValueNULL.

Kotlin 第一次如何管理这种比较?

谢谢你的帮助。

0 投票
1 回答
150 浏览

android - 为什么我们可以设置“var name = null”而没有错误

我刚开始学习 Kotlin,发现了一个我无法理解的关于 Kotlin 中的 Null Safety 的问题。我读到该变量不能保存空值。

所以我对此做了一些测试

直到我尝试这个

我不知道为什么我可以设置var name = null没有错误。

0 投票
2 回答
93 浏览

java - 如何在没有 Java 的纯 Kotlin 中实现接口 null vs throw dispatch

考虑这个例子:


CommonHandler.java

具有附加逻辑的通用处理程序。这里非常简化:


NullableHandler.kt

null如果操作抛出异常,则返回的处理程序版本。结果是类型R?


ThrowingHandler.kt

将错误包装在其内部异常类型中的处理程序版本。结果类型为R.


api.kt

基本上任何我们不拥有且无法修改的 API。


现在,有了以上所有类,我们就可以实现 API 包装器了:


我的问题是,我怎样才能在不回退到 Java的情况下实现类似的层次结构,这样就有一个类/接口与可以返回R类型或R?类型的方法。据我所知,我们不能用R!语法在 Kotlin 中显式声明平台类型。

0 投票
2 回答
133 浏览

kotlin - 什么是正确的使用!! 操作员?

我理解 Kotlin 的!!操作符是做什么的。我想知道是否有任何用例可以更好地!!代替?.letor?: return或其他更安全的空检查。

关于 null 安全性的Kotlin 文档只是说它适用于喜欢 NullPointerExceptions 的人;人们为什么要那些?

我主要只是好奇它是否有用例,没有更好的选择。

0 投票
1 回答
107 浏览

kotlin - 为什么 kotlin 提供空安全运算符而不提供异常安全运算符?

如果 kotlin 提倡安全代码,例如:

为什么没有类似的异常处理语法,例如使用虚??.运算符:

我没有看到这里的缺点。有什么理由这不是语言的一部分吗?

0 投票
0 回答
159 浏览

kotlin - Is it bad practice to chain Kotlin null safety operator when dealing with immutable variables and properties?

Take the hypothetical pseudo-code below as an example:

I've read an article recently that suggested this kind of usage of the null safety operator creates redundant null checks when compiled into Java machine code. Here -- according to that article -- it would seem that when this is compiled, when a != null evaluates to true it will essentially check if a != null three times on the same variable. It might make since when you are dealing with a var property that has the potential to change values between checks. But in situations with immutable val properties, this creates redundant checks. If the above example is executed on each element over an enumerable data structure, this will scale the redundant checks linearly, making the end result 3 times more computationally complex than it needs to be.

Syntactically it makes a lot of sense to use Kotlin in this way. But if it produces wasteful/redundant checks like this, it seems like this is better to avoid this by explicitly null checking the variable with an if statement.

Is it appropriate to chain usages of null safety operator, especially when combined with lambdas that would check the same immutable variable multiple times? Or in other words, will doing so really result in excessive/redundant null checks in compiled java machine code?

0 投票
2 回答
202 浏览

android - 创建新的 User 对象时如何避免 KotlinNullPointerException?

我在 Firebase 中有这个身份验证代码:

这是我的用户类:

KotlinNullPointerException在突出显示的行上得到了一个。构造函数的调用怎么会产生这个异常呢?我怎样才能避免它?