Listing 6.8 Using double-bang operator - example only prints null

I got this error:
Error:(36, 13) Kotlin: Null can not be a value of a non-null type String
running this code:

var drink = readLine()!!.capitalize()
drink = null

Why does the compiler not use String? type automatically?

Furthermore this listing seems to be a wrong example, again.
Because the line
beverage = null
is beyond the .capitalize function call, the listing only prints null instead of throwing a null pointer exception. If the beverage = null would be placed before
var beverage = readLine()!!.capitalize()
the capitalize function would use the input from readLine() and then the null assigned to beverage before would be overwritten with this capitalized input.

This is throwing an null pointer exception for me:

var drink:String? = null

Best Regards

Don’t much about the context to your question but base on your code snippet the complier is using the value of readLine(). Base on the docs it will return a string value or null since its signature is String?. Here is the link:

for the first block the using the “!!” tells the complier that you are okay with passing null knowing you might get a NPE. The capitalize() function takes a String and has no null checks. if you used readLine()?. instead of readLine()!! the NPE would have been avoided.

The same concept applies for your second code snippet. The reason is again capitalize() takes a String instead of String? as its parameter.

hope this helps.