scala null指针异常原因是在不为null的变量上进行。
谁能在Scala中解释以下行为?
基本上在以下代码中,如果您执行getOptionValue [int](none)
,它将返回0,但是当您执行getOptionValue [int](none)时,.toString
导致无效指针异常。
结果不应该将0铸成字符串(即。“ 0”)?
PS:我使用的是Scala 2.12.10
scala> def getOptionValue[T](option: Option[T]): T = {
| if (option.isEmpty) {
| null.asInstanceOf[T]
| } else
| option.get
| }
getOptionValue: [T](option: Option[T])T
scala> getOptionValue[Int](None)
res35: Int = 0
scala> res35.toString
res36: String = 0
scala> getOptionValue[Int](None).toString
java.lang.NullPointerException
... 47 elided
,下面的工作正常
scala> null.asInstanceOf[Int]
res0: Int = 0
scala> null.asInstanceOf[Int].toString
res1: String = 0
Can anyone please explain the following behavior in scala?
Basically in the following piece of code, if you do getOptionValue[Int](None)
, it returns 0 but when you do getOptionValue[Int](None).toString
it causes a null pointer exception.
Shouldn't the outcome be 0 cast into a string (ie. "0")?
PS: I am using scala 2.12.10
scala> def getOptionValue[T](option: Option[T]): T = {
| if (option.isEmpty) {
| null.asInstanceOf[T]
| } else
| option.get
| }
getOptionValue: [T](option: Option[T])T
scala> getOptionValue[Int](None)
res35: Int = 0
scala> res35.toString
res36: String = 0
scala> getOptionValue[Int](None).toString
java.lang.NullPointerException
... 47 elided
Also the below works just fine
scala> null.asInstanceOf[Int]
res0: Int = 0
scala> null.asInstanceOf[Int].toString
res1: String = 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些:
相反(不可靠的)例外。
在JVM上调用
null
引用最终以nullpoInterException
最终调用任何方法。Scala不会将
scala.int
始终为java.lang.integer
(可以无效),也不会始终将其视为int
((原始的,不可用设计拆卸) - 它基于上下文使用任何一个表示。如果
null.asinstanceof [int]
Scala实际上将执行转换而不是铸造,这就是为什么您将获得0
。但是,只有知道铸型类型(null
)和目标类型(int
),它才能执行此铸件技巧。这就是为什么当您使用仿制药时,Scala只会知道有
null
铸造到某些参考类型t
时,方法定义对t不了解
为int
,因此它不会执行转换,它将仍然是null
从该方法返回的,然后它将在上抛出NPE。 ToString
。首先存在这些例外的原因是,当您有这样的情况时,可能会感到难过:
编译器很可能会使用
int
,铸造没有意义,因此他们选择了转换原始将获得值0
而不是编译或运行时错误。但是,只有在您知道自己的目标是原始类型(int
,double
,char> char
,.. )。These:
are rather (unreliable) exceptions.
On JVM calling any method on
null
reference ends up withNullPointerException
.Scala doesn't treat
scala.Int
always asjava.lang.Integer
(which can be nullable) nor it always treat it asint
(primitive, non-nullable by design) - it uses either representation based on context.In case of
null.asInstanceOf[Int]
Scala will actually perform a conversion rather than casting, which is why you'll get0
. But it can only perform this casting when it knows the cast type (Null
) and the target type (Int
), as after the type-erasure it doesn't do such tricks.Which is why when you are using generics, Scala will only know that there is
null
cast to some reference typeT
, method definition doesn't know anything aboutT
beingInt
, so it won't perform conversion, it will still benull
returned from the method, and then it will throw NPE on.toString
.The reason these exceptions exist in the first place is that it probably felt bad that when you have case like:
compiler would most likely use
int
, casting wouldn't make sense, so they opted for conversion so that primitive would get the value0
rather than compile or runtime error. But this can only be done when you know that you are targeting something that can be a primitive type (Int
,Double
,Char
, ...).