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

边栏推荐
- wallys/600VX – 2×2 MIMO 802.11ac Mini PCIe Wi-Fi Module, Dual Band, 2,4GHz / 5GHz QCA 9880
- Database cascading operation
- R语言查看版本 R包查看版本
- Using cookie technology to realize historical browsing records and control the number of displays
- Kongsong (ICT Institute) - cloud security capacity building and trend in the digital age
- 一个人就是一本书
- ClipboardJS——开发学习总结1
- 10 days to learn how to flutter Day10 flutter play animation and packaging
- wallys/IPQ8074a/2x(4×4 or 8×8) 11AX MU-MIMO DUAL CONCURRENT EMBEDDEDBOARD
- 再不上市,旷视科技就熬不住了
猜你喜欢

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

Is the golden cycle of domestic databases coming?

Stm32f407zgt6 uses SDIO mode to drive SD card

wallys/IPQ8074a/2x(4×4 or 8×8) 11AX MU-MIMO DUAL CONCURRENT EMBEDDEDBOARD

The first batch in China! Alibaba cloud native data Lake products have passed the evaluation and certification of the ICT Institute

基于视觉的机器人抓取:从物体定位、物体姿态估计到平行抓取器抓取估计

优惠券种类那么多,先区分清楚再薅羊毛!

Boost study: boost log

EMC surge

60 个神级 VS Code 插件!!
随机推荐
暑假学习记录
QT embeds the sub QT program window into the current program
dplyr 中的filter报错:Can‘t transform a data frame with duplicate names
据说用了这个,老板连夜把测试开了
Database connection pool Druid
goto语句跳转未初始化变量:C2362
The latest collection of arouter problems
Object mapping - mapping Mapster
EMC surge
缓存雪崩和缓存穿透解决方案
R语言ggplot2可视化分面图(facet):gganimate包基于transition_time函数创建动态散点图动画(gif)、使用labs函数为动画图添加动态时间标题
Webview,ScrollView滑动冲突咋整
Alibaba cloud lifeifei: China's cloud database has taken the lead in many mainstream technological innovations abroad
Flutter start from scratch 008 form
【重温经典C语言】~c语言中%x、%c、%d、%x等等等、c语言取地址符&的作用、C语言中的 联合体
【模式识别大作业】
Database transactions
Digitalization is not a trial, but a wading out of "Xingzhi Digital China" × History of Foxconn
Qualcomm released the "magic mirror" of the Internet of things case set, and digital agriculture has become a reality
R语言查看版本 R包查看版本