当前位置:网站首页>Another miserable day by kotlin grammar
Another miserable day by kotlin grammar
2022-06-30 11:52:00 【Sharp surge】
The source is a feedback from colleagues , A... Was reported during the test Kotlin.Lazy Null pointer exception for ,Lazy Is defined as follows :
class TestA{
...
val xxxx:Service? by lazy{
xxxService()
}
...
}
It looks very ordinary by lazy Why do I report null pointer ? In the deep lazy When viewing the source code , No suspicious points were found , At that time, the code logic involved concurrent calls , I checked it, too by lazy The initialization , The default implementation is SynchronizedLazyImpl, Thread safe operation has been done .
To avoid too much code interference , We will cover by lazy All the places used are copied to one Test Class , And then through Decompile Decompile to Java Code to see if it is kotlin The problem of .
Kotlin The code is as follows :
class TestA {
init {
....
initView()
}
private fun initView() {
// call Service Method
service?.getName()
}
private val service: AService? by lazy {
AService()
}
}
Decompiled Java Code :
public final class TestA {
private final Lazy service$delegate;
private final void initView() {
// 1、 obtain service example
AService var10000 = this.getService();
if (var10000 != null) {
var10000.getName();
}
}
private final AService getService() {
Lazy var1 = this.service$delegate;
Object var3 = null;
// 2、 call Lazy Of getValue Method
.return (AService)var1.getValue();
}
public TestA() {
this.initView();
// 3、 initialization Lazy example
.this.service$delegate = LazyKt.lazy((Function0)null.INSTANCE);
}
}
Immediately find the problem through the decompilation of the code :
1. stay TestA In the construction method of , Execute first initView Method to get AService Example .
2. but getService Methods Lazy It's not initialized yet , But it directly calls getValue Method triggers a null pointer exception .
3. stay initView Do it after you finish Lazy The initialization , It's too late , The exception has already occurred .
How to solve this problem ? Just put the by lazy Referred to the init In front of the code block , as follows :
class TestA {
private val service: AService? by lazy {
AService()
}
init {
initView()
}
...
}
Decompilation result :
public final class TestA {
private final Lazy service$delegate;
....
public TestA() {
// 1、 initialization Lazy example
.this.service$delegate = LazyKt.lazy((Function0)null.INSTANCE);
// 2、 Call again getService Method
.this.initView();
}
}
1. Construction is finally initialized first Lazy object .
2. Call again initView Method , At this time, the method is Lazy.getValue Can be called normally .
Is it a little against common sense ? Why calling a variable in a method also involves the location of the variable ,Kotlin I'm afraid this advanced grammar candy is even C None of them ( Laugh at , ha-ha ).
that Kotlin Is there really no grammar check on it ? There is , Let me change the code to show you :
IDE Will prompt the current service uninitialized ,「 However, this prompt is limited to init Call in code block lazy When it's time to remind , If in init Call an intermediate method in , Then call from the intermediate method lazy, The prompt verification will be invalid 」.
Has been Kotlin Grammar sugar pit miserable day !!!
The author has a top , Put them together to learn ~
边栏推荐
- Goto statement jump uninitialized variable: c2362
- 建立自己的网站(13)
- The operation and maintenance security gateway (Fortress machine) of Qiming star group once again won the first place!
- 数据库连接池 druid
- 科普达人丨漫画图解什么是eRDMA?
- R语言ggplot2可视化:使用ggplot2可视化散点图、在geom_point参数中设置alpha参数指定数据点的透明度级别(points transparent、从0到1)
- R language view version R package view version
- 「运维有小邓」用户个人资料管理
- Qt嵌入子Qt程序窗口到当前程序
- 揭秘得物客服IM全链路通信过程
猜你喜欢
In depth analysis of Apache bookkeeper series: Part 4 - back pressure
他是上海两大产业的第一功臣,却在遗憾中默默离世
led背光板的作用是什麼呢?
koa - 洋葱模型浅析
Alibaba cloud lifeifei: China's cloud database has taken the lead in many mainstream technological innovations abroad
Qualcomm released the "magic mirror" of the Internet of things case set, and digital agriculture has become a reality
CVPR 2022 | greatly reduce the manual annotation required for zero sample learning. Mapu and Beiyou proposed category semantic embedding rich in visual information
He was the first hero of Shanghai's two major industries, but died silently in regret
一个悄然崛起的国产软件,低调又强大!
国内首批!阿里云云原生数据湖产品通过信通院评测认证
随机推荐
Multiparty Cardinality Testing for Threshold Private Set-2021:解读
Multiparty cardinality testing for threshold private set-2021: Interpretation
构造函数、类成员、析构函数调用顺序
揭秘得物客服IM全链路通信过程
Customize an annotation to get a link to the database
CVPR 2022 | greatly reduce the manual annotation required for zero sample learning. Mapu and Beiyou proposed category semantic embedding rich in visual information
基于视觉的机器人抓取:从物体定位、物体姿态估计到平行抓取器抓取估计
Speech signal processing - Fundamentals (V): Fourier transform
shell第一个命令结果传入第二个命令删除
Cache avalanche and cache penetration solutions
"War" caused by a bottle of water
科普達人丨漫畫圖解什麼是eRDMA?
R language view version R package view version
Dameng data rushes to the scientific innovation board, or becomes the "first share of domestic database" in the A-share market
深入解析 Apache BookKeeper 系列:第四篇—背压
使用cookie技术实现历史浏览记录并控制显示的个数
Shell first command result is transferred to the second command delete
AUTOCAD——LEN命令
There are so many kinds of coupons. First distinguish them clearly and then collect the wool!
TypeScript ReadonlyArray(只读数组类型) 详细介绍