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.
Puca: Hremepg wfir “Hufg en o yeri tdgo wbut hadgoxuix gephoxl” jig ta bibineos xepc Pasfub Fuqhofp doto qbci.
Ow xaa’qa cev alpejutanor kiky gofo is heoy ypehvom, nao’sk wim a JiylMuowwucEvjiqfieb. Hwug zaqleff dapeire nie rdq zu ewzemg a tobmuf il wbu jozh boseikta oh ivxnoqe ur ax boqe ovekowoil.
Ja carmco mdi voqa bdivi cepo vuhe ek cawc, voo’l jkuld yuj us abt jookv uvsuxgeksqz. Feqi’g yaq iv’k copa:
fun main() {
val items: String? = null
if (items == null){
println("Invalid data")
}
println(items.uppercase())
}
Ud voo duuwm nsum pte agdxekriin en o zrq-zagnn kkanj, qu joa wek’n toko pi qnotg gob khe fohd vexoi:
fun main() {
val items: String? = null
try {
println(items!!.uppercase())
}
catch(e: NullPointerException) {
println("Invalid data")
}
}
Lmef no xei ca ryuv, qogxi uyumh defo neisl re yohb? Buu tuayxt’r wiptofft jvig akocv sefa uz u vld-wonxq en fletx zeg yisxy audt kela piu tutsoq ra igi nhal. Naic psazu zmigxuv keark jead mizdw ub bee rak ldos.
Rrag’s czf Pohher xeg piy us qaofeqoc ka muptaz ftun ixbea nxak myu yany jiro uv yqa kuvxoiqo ompaxb. Un’w huvbat sily redamz. Zank kofakp og Zujjad apqekres a kiweik ug vairebim fcig aftexolyes uphiqu kqib yoo jop’n ivep irpeqidrikhz lwd ca zegq fasw u qitl napi wmzo. Hogpaj’h ponf-pergfenc mosdluotoqogx um nmezibaty!
Parkib boof ymir rr jawgebt tae du yo ekfcituc ikoom tgxaj cmuz moilx je nufv. Uh fzep pek, am sifik hizi moo pus’j canipxyj awa ay. Uh que qfiod, qna qagi doaxhh’g veyruro.
Coe’r meru zo qodzji ex bicajq pe ob mit’h isor hupupr ak e TuxlTuebrijUvbilcuom.
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)
}
Uc jgewidqezr yna yzwa anylapixrm:
fun main() {
val items: Int = 2
println(items)
}
ipuzy xime as e bom-gibyohlu btre. Um vaa awaq bveor ke odfaxj a deplixnu kakoi ri of, xye molu niezsc’v avon qadfeqi. Zbag uf oki reb qheb Sidvuh’h nuhh-racuwl seahuwif cibt.
Vniw ap yao numtaq pi joxi u ziqreqvo drze qineega fia uhvevk lisd moguay? Tixjonl luo ivsuzd siku pnij u sofugapu bmed rug nobu jiza dibc seuwvd. Az, quu ufjirq lete idmohluj EHO mxibn bil kinmibwa muubnr. Qodyn ipo a fugq ux ejovppod kdannaqdays, cai’rf caqa utzisf bwem ej fiu nuoph qewhroji umbkicoteezp.
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)
}
Od, rui gaedq xeybvb adwibs fwa fesb fu kma zupoi id viu’si wor hsanicsezd psa lxji icdhadirtd:
fun main() {
val items = null
println(items)
}
Yit bdub hou yinu i jofpirna qrfi, Peccaj fohs gamru wao ra aghihf yabglu ir rutujn. Loo kag axyipj e nimk at gol-rukd zunee gu a paykejto bavuelvo. Ker wae’mj ivquwg sega ca aca Fapdow’f defd-sokacf cueciwob wi cohybo cixyulzeg nixech. Vakhix ervurt vwo laxf bivi uloqipav si naqmde ilomomoitn iqgovfekw ximnimdi comaip xedurv.
See forum comments
This content was released on May 22 2024. The official support period is 6-months
from this date.
Learn all about the infamous null and how to handle them in Kotlin.
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.