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

边栏推荐
猜你喜欢

"New digital technology" completed tens of millions of yuan of a + round financing and built an integrated intelligent database cloud management platform

The operation and maintenance security gateway (Fortress machine) of Qiming star group once again won the first place!

wallys/3 × 3 MIMO 802.11ac Mini PCIe Wi-Fi Module, QCA9880, 2,4GHz / 5GHzDesigned for Enterprise

What is the function of LED backlight?

限时预约|6 月 Apache Pulsar 中文开发者与用户组会议

R language view version R package view version

Handler source code analysis

Who still remembers "classmate Zhang"?

他是上海两大产业的第一功臣,却在遗憾中默默离世

EMC surge
随机推荐
Database transactions
HMS Core音频编辑服务3D音频技术,助力打造沉浸式听觉盛宴
Re understand oauth2.0 protocol for joint login
限时预约|6 月 Apache Pulsar 中文开发者与用户组会议
Customize an annotation to get a link to the database
Uncover the whole link communication process of customer service im
R语言去重操作unique duplicate filter
c# 怎样能写个sql的解析器
Review the writing software with characteristics
60 个神级 VS Code 插件!!
ClipboardJS——开发学习总结1
再不上市,旷视科技就熬不住了
WebView, Scrollview sliding conflict correction
19年来最艰难的618,徐雷表达三个谢意
Kongsong (ICT Institute) - cloud security capacity building and trend in the digital age
10 days to learn how to flutter Day10 flutter play animation and packaging
It is said that with this, the boss opened the test overnight
论文解读(AGC)《Attributed Graph Clustering via Adaptive Graph Convolution》
It's time for the kotlin coroutine to schedule thread switching to solve the mystery
A man is a Book