当前位置:网站首页>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 ~

边栏推荐
- Summer vacation study record
- Uncover the whole link communication process of customer service im
- Let's talk about how to do hardware compatibility testing and quickly migrate to openeuler?
- Flutter start from scratch 008 form
- [applet practice series] Introduction to the registration life cycle of the applet framework page
- 他是上海两大产业的第一功臣,却在遗憾中默默离世
- Webview,ScrollView滑动冲突咋整
- Alibaba cloud database represented by polardb ranks first in the world
- 60 divine vs Code plug-ins!!
- Le talent scientifique 丨 dessins animés qu'est - ce qu'erdma?
猜你喜欢

Win10 R package installation error: not installed in arch=i386

数字化不是试出来,而是蹚出来的|行知数字中国 × 富士康史喆

Multiparty Cardinality Testing for Threshold Private Set-2021:解读

谁还记得「张同学」?

19年来最艰难的618,徐雷表达三个谢意

led背光板的作用是什么呢?

Alibaba cloud lifeifei: China's cloud database has taken the lead in many mainstream technological innovations abroad

Le talent scientifique 丨 dessins animés qu'est - ce qu'erdma?

Filter error in dplyr: can't transform a data frame with duplicate names

Analysis of KOA - onion model
随机推荐
重新理解oauth2.0协议进行联合登录
Analysis of KOA - onion model
Multiparty Cardinality Testing for Threshold Private Set-2021:解读
Learn how to implement distributed locks in redis - my own understanding
In depth analysis of Apache bookkeeper series: Part 4 - back pressure
He was the first hero of Shanghai's two major industries, but died silently in regret
OpenMLDB Meetup No.4 会议纪要
Database connection pool Druid
wallys/3×3 MIMO 802.11ac Mini PCIe Wi-Fi Module, QCA9880, 2,4GHz / 5GHzDesigned for Enterprise
优惠券种类那么多,先区分清楚再薅羊毛!
R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)
Object mapping - mapping Mapster
win10 R包安装报错:没有安装在arch=i386
1175. 质数排列
国内首批!阿里云云原生数据湖产品通过信通院评测认证
揭秘得物客服IM全链路通信过程
Oracle netsuite helps TCM bio understand data changes and make business development more flexible
shell第一个命令结果传入第二个命令删除
Automatic database growth
Let's talk about how to do hardware compatibility testing and quickly migrate to openeuler?