Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "Required:String Found:String?" Kotlin and Android Studio

As the title suggests Im getting a red underline under "id" on the line "var myNote = Note(id, title, note, ServerValue.TIMESTAMP)" Error "Required:String Found:String?" Kotlin and Android Studio

class MainActivity : AppCompatActivity() {
    var mRef:DatabaseReference? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val database = FirebaseDatabase.getInstance()
        mRef = database.getReference("Notes")

        add_new_note.setOnClickListener {
            showDialogAddNote()
        }
    }

    fun showDialogAddNote() {
        val alertBuilder = AlertDialog.Builder(this)

        val view = layoutInflater.inflate(R.layout.add_note, null)

        alertBuilder.setView(view)

        val alertDialog = alertBuilder.create()
        alertDialog.show()

        view.btnSaveNote.setOnClickListener {
            val title = view.etTitle.text.toString()
            val note = view.etNote.text.toString()

            if (title.isNotEmpty() && note.isNotEmpty()) {
                var id = mRef!!.push().key

                var myNote = Note(id, title, note, ServerValue.TIMESTAMP)
                mRef!!.child(id).setValue(myNote)
                alertDialog.dismiss()    
            } else {
                Toast.makeText(this, "Empty", Toast.LENGTH_LONG).show()
            }
        }
    }
}

Here is my Notes.kt class

package com.example.gearoidodonovan.books

import java.util.*

class Note (var id:String, var title:String, var note:String, var timestamp: MutableMap<String, String>) {
}
like image 902
Gearoid O Donovan Avatar asked Jan 22 '19 15:01

Gearoid O Donovan


2 Answers

Kotlin forces you to be hyper-conscious about nullability.

Your Note entity say its requires a non-nullable id:String, and apparently, mRef!!.push().key returns a String? meaning it's a nullable String. You can fix this by double-banging it , i.e. mReff!!.push().key!!

Another tip is to ALT+ENTER these Kotlin related errors, it'll provide the double-bang for you.

like image 143
ElliotM Avatar answered Oct 20 '22 04:10

ElliotM


Your id property in Note is declared as non-null String, while the key you have is a potentially null String?. You need to bridge this gap.

  1. The simplest but most dangerous way is to use !!, which will produce an exception if the key was null, i.e.

    var id = mRef!!.push().key!!
    
  2. A better way is to handle the null case somehow, for example by performing a null check:

    var id = mRef!!.push().key
    if (id != null) {
        var myNote = Note(id, title, note, ServerValue.TIMESTAMP)
        mRef!!.child(id).setValue(myNote)
        alertDialog.dismiss()
    } else {
        // handle the case where key is null somehow
    }
    
  3. You could also make the property in your own class nullable, and deal with the ID value in there potentially being null later:

    class Note (var id: String?, var title: String, var note: String, var timestamp: MutableMap<String, String>)
    

Note that all the mRef!! calls are problematic as well. For one, Hungarian notation (the m prefix) is generally discouraged in Kotlin, and the !! operator is dangerous as well. You'd be better off handling the null case early for that reference, and then you could use it more conveniently, without having to litter your code with !!.

I also encourage you to read up on null safety in general the official documentation or in this answer.

like image 2
zsmb13 Avatar answered Oct 20 '22 03:10

zsmb13