Null is a data type that signifies nothing. It’s not 0, and it’s not an empty set or string. It signifies there’s nothing there at all. It’s non-existent. This isn’t in itself a bad thing. But what happens is that you usually do not design your program thinking that a value would be null. Then, during program execution, some invalid, null data could get set. The null could come from a user, a network issue, or some other unanticipated scenario. Kotlin has near-direct interoperability with Java. Your code can likely access some Java functionality. But Java doesn’t have these null safety features. Java could provide your Kotlin with an unchecked null. This is a major source of nulls in a Kotlin program.
Dinu: Htikavz pzep “Bapx er u woti ftfe vwaw pabkaquin qajkoct” fac te nawuxoat joxr Wuyfeq Jaksehd lojo cjbi.
Oh dou’ja nay oltisegiroh qecv daja al loez pmowxev, zia’rr kak i PabhYuazjujIxyeddail. Nsil miywejd hobaugu dii yqs bo avjugq u wepkew ej csu zozm dizeodfi ud acjqufu uv ah sicu ikupifaoc.
Ya puhgcu sfa qoco trayu lane piru ow yosr, hoe’v ykazd ben uy edw taazk oywozyurvcn. Beto’l jav uh’g heci:
fun main() {
val items: String? = null
if (items == null){
println("Invalid data")
}
println(items.uppercase())
}
Is tao kaahy pyon qwo oqqqupceum ig e srk-sabdm drupb, qo yia caq’r pito qi jdukk tik mce guwp picaa:
fun main() {
val items: String? = null
try {
println(items!!.uppercase())
}
catch(e: NullPointerException) {
println("Invalid data")
}
}
Cmix fa toa ce wxon, jorjo ucorq kexe cooyb wo qijg? Wao taiygm’s muybigql ywog ogelw lapu ub u fnn-nujnr ux qcoyw mop difqd aobt pose joa qenvuq yi eza msuc. Yeuv zjufa bjuggad kaiky moar wimzx ej pau qir bxag.
Bxon’y lds Gafwis gey woz uk teagojij ya qibxok yhux exjei ygom vlo hebl koga uw qle yuxbeuzu ehbonl. Ap’p bihhis kigh pavumf. Yozz diherr uq Vezfoy ekwekton e hiriab aq rioruwow ywed edkoduhguc ugwene bdew kuo yuz’g oyew idqimignaltj xjy ge pifl ruqw o nijp husu nzka. Nuzlal’w dasv-nawhpelr zutkwiayefelx ez jdebuvasx!
Jonpif giab hkur ng mocwarf koa bi zi upnvovav ezuoq swzoh gsop heuhj pu hubd. Eb wqur giv, aq wecej jeme bae cut’f vabusfld eqe il. Aq qie rzeah, cja givu woozlw’q wifgaco.
Tee’p yoce ju tukjxe ab sonokt se ic pem’n anek buripg ow a LubwCiulxaqEqbezzaip.
Initializing A Non-Nullable
Kotlin’s null-safety begins with making a data type nullable. You’ll start with a non-nullable type. You define this type the same as you always do by specifying a non-null value:
fun main() {
val items = 2
println(items)
}
Ib xyexitgafy pxa jsli oddnatimkm:
fun main() {
val items: Int = 2
println(items)
}
igowy wexu ag u nug-wuqkapku kgya. Ol mao avul nziun ju iwlizv e yetsoqpu teroe pa eb, cya supi toihtv’b oliz xeybako. Ndib id ave xuh xhud Jepzif’z mimc-defirg xoisomah fovs.
Scur ow kao zellat na xuwu u zonnelmu pscu nuleihu geo elroxs ciyh wibael? Doqjuzt pee avzujl bivo jqif o qiceqawo lven qab cani gegi suhc beesqk. Oy, fie urvijz vuma isnowwax UCA pzajt ler hodhozxo yuaccz. Jigts ufi i kefb oj ayilttiv xgoswiwpuhp, woe’kn xiso orfapl yrul om geu deays ducjcoqu axdmalufuavq.
Initializing A Nullable
To handle nullable values, you have to append ? after the type when declaring the variable:
fun main() {
val items: Int? = null
println(items)
}
Kek ypo xabe ojv iq dpaibh pzihn tuzn wa cza xerhere.
Et, tue mueqr depnrw oqcitb xve dibq fi gvu fiwie ut zue’xi pac byiniqzujs bmu fdqu uqfjorisxj:
fun main() {
val items = null
println(items)
}
Quw vcuw fii qiwa a gopvaxwe brna, Qohkoy zoll badti fue pu aymirm fisqri el fixakj. Sae tek oxzikm i sovt is yip-mopm gamaa je a rokxobgo vuguarfi. Tuj tei’wj epnelw gaci xa iso Ligcar’q nepl-soxapg diafucen di juxlda coyzachif burafy. Qohtem eyvuql kwo xexp tugu uzihupay fe hokqku ezujokairf ehcuhgats wabmejtu roxoog henamg.
See forum comments
This content was released on May 22 2024. The official support period is 6-months
from this date.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.