Skip to main content

Null or not null

I did the easy thing - I converted my entire project to kotlin. A StackOverflow question told me that I need to go to Code menu in Android studio and give the option "convert java to kotlin".

As simple as that. I opened each and every java source file and converted them into .kt file.

Then came the fun part - hundred of errors. Because this automatic conversion does not consider the subtle points of your code.


override : In java, when an inherited class redefines a base class function, it uses @Override before the function name. But kotlin uses "override". Automatic conversion does not convert @Override to override. You will have to do it yourself.

Null, not null  and null safety : Kotlin is very particular about null safety. It uses the following ? and !! for this purpose.

A variable defined as ? in front of its type name is nullable.

e.g.

var str1:String="Hello world"
var str2:String?="Hello world"

str2 is a nullable string. And str1 is not nullable string.

So when you convert a java program in Android studio to kotlin, many of the Activity class methods use nullable parameters and hence the overriding methods also need to specify ? in front of the parameter type.

e.g.
override
fun onCreate(savedInstanceState:Bundle?){}

So we are specifying that the parameter savedInstanceState can be null.

Unless and until specify this nullable operator (?), the IDE keeps telling you that there is an "accidental override error. "

There is an operator !! which will convert any type to non-null type if possible and throws an exception if it is null.

What I have been doing so far is try ?, if it does not work, try !!. And if that fails as well shut down the IDE.

And I have taken more than a week (7 working days and 3 hours a day :) ), so far and still keep finding plenty of red lines in these kt files. Am I going in Circles? Here just like in life?



Comments

Popular posts from this blog

ಧರ್ಮದ ಉದಯ

 ಇಷ್ಟು ವರ್ಷಗಳ ನಂತರ ನನಗೆ ಒಂದು ವಿಷಯ ಸ್ಪಷ್ಟವಾಗುತ್ತಾ ಇದೆ. ಧರ್ಮ-ದೇವರುಗಳ ಮೂಲ ಸಂಸ್ಥಾಪಕಿ ನಮ್ಮ-ನಿಮ್ಮ ಹಾಗೆ ವಯಸ್ಸಾದ ಮಹಿಳೆ. ಜೀಸಸ್ ಅಲ್ಲ, ಬುದ್ಧ ಅಲ್ಲ, ಮಹಮದ್ ಅಲ್ಲ, ಬಸವಣ್ಣನಲ್ಲ.  ನೀವು ಖಂಡಿತ ಒಪ್ಪಿಕೊಳ್ಳವದಿಲ್ಲ ತಾನೇ? ಸ್ವಲ್ಪ ವಿಶ್ಲೇಷಿಸಿ ನೋಡೋಣ. ಶಾಂತಮ್ಮ - ಹಾಗೆಂದು ಹೆಸರಿಡೋಣ ನಮ್ಮ ಸಂಸ್ಥಾಪಕಿಗೆ. ಅವಳಿಗೆ ಚಾಲೀಸು, ಪಚಾಸು ಎಲ್ಲ ಯಾವಾಗಲೋ ದಾಟಿದೆ. ಮಕ್ಕಳು-ಮೊಮ್ಮಕ್ಕಳೆಲ್ಲ ದೊಡ್ಡವರಾಗಿ ಅವರವರ ಗೂಡು ಸೇರಿದ್ದಾರೆ. ಮನೆಯಲ್ಲಿರವವರು ಅವಳು, ಗಂಡ. ಎಲ್ಲೋ ಒಂದಿಷ್ಟು ಜಿರಲೆ, ಹಲ್ಲಿ, ಇರುವೆಗಳು ಕಂಪನಿ ಕೊಡುತ್ತಿವೆ. ದಿನದಲ್ಲಿ ೩-೪ ಗಂಟೆ ಅಡಿಗೆ-ಕಸ-ಮುಸುರೆ ಮಾಡಿದ ನಂತರ ಅವಳ ಕೈ, ಮನಸ್ಸು ಎಲ್ಲಾ ಖಾಲಿ. ಟಿ.ವೀ., ಮೊಬೈಲ್ ನೋಡುತ್ತ, ಬೇಜಾರಾದರೆ ಗೋಡೆ ನೋಡುತ್ತ ಕುಳಿತು ಅಲ್ಲೇ ನಿದ್ದೆ ಬಂದಿದೆ. ಅರೆ ನಿದ್ರೆ, ಅರೆ ಕನಸಿನಲ್ಲಿ ಏನೋ ನೋಡುತ್ತಾಳೆ - ಅದು ನಿಜವಾದ ಕನಸಾ, ಅಥವಾ ನೋಡಿರುವ ಸಾವಿರಾರು ಸೀರಿಯಲ್ಲುಗಳ ಕಲಸುಮೇಲೋಗರವಾ ಗೊತ್ತಿಲ್ಲ. ಒಂದು ವ್ಯಕ್ತಿ ಕಾಣುತ್ತಾನೆ - ಅವನ ತಲೆಯ ಸುತ್ತ ಬೆಳಕು (ಅಷ್ಟು ಹೊತ್ತಿಗೆ ಒಂದರೆಕ್ಷಣ ದೊಡ್ಡ ಬಿಸಿಲು ಬಂತಾ ?). ಅವನು ಏನೋ ಹೇಳುತ್ತಿದ್ದಾನೆ. ಅವಳಿಗೆ ಸರಿಯಾಗಿ ಕೇಳಿಸುತ್ತಿಲ್ಲ. ಕೊನೆಯಲ್ಲಿ ಅಭಯ ಹಸ್ತ ತೋರಿಸಿ ಮಾಯವಾಗುತ್ತಾನೆ. ಪೊಲಿಟಿಶಿಯನ್ನಾ? ಎದ್ದ ಶಾಂತಮ್ಮ ಕನಸು ನೆನಪಿಸಿಕೊಳ್ಳಲು ನೋಡುತ್ತಾಳೆ - ಎಲ್ಲಾ ಅರೆಸ್ಪಷ್ಟ. ಏನಿರಬಹುದು - ಯಾ...

Dharmabheeru

 I wanted to call this post 'fear'. Then something told me I might have at least one more post with that title. And of course there it was.  So the title had to be changed to Dharmabheeru (dharma - religion, bheeru - fearful, timid) - god fearing - pious. You are afraid. But that is not a bad thing because you are afraid of God - that he may punish you if you commit a sin.   But I have a feeling that fear makes us religious, makes us believe in God, because we want to be protected by someone - and mom can't protect the adult us.   Drowning myself in Sanskrit works - particularly Ashtavakragita ,  I had to find some meaning of these words like atma, mukti, jnana. And I asked AI to find me some texts to understand history of religions. It gave me many options - most of which are not available. But the one I stuck to was "The natural history of religions".  Reading that text was not very easy - but 3 part series of podcasts by 'Philosophize this' ab...

Youtube achcha hai

 Traveling in the last seat - berth of a bus is liking being swung in a cradle. By a drunk and angry mom. There are no Indian drunk and angry moms - we would like believe. But still the bus ride was exactly like that.  Twists and turns and twists and turns. Luckily there were no jumps - unlike older buses. May be a better suspension system.  The good old insomniac with some weird form of social anxiety traveling in such a seat. Ah, what a hell.  It is not that I was unprepared. I did download two episodes of funny Netflix show - I had charged both the phone and tablet to 100%. I must have some how enabled location services - phone battery was dying quite fast.  I tried watching those Netflix episodes - one hour was spent. I tried solving a simple 6X6 sudoku. Another 10 minutes was spent.  Neend na mujhako aaye - dil mera ghabaraye. Funny I didn't even remember that song then.  Finally I decided to take refuge of much detested youtube. I also added 1 GB...