0

I try to make my first app, but ı have got the error message. The error message is:

2020-09-17 11:18:17.846 30034-30132/com.ibrahimkiceci.simplynoteapp W/i.simplynoteap: Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)

2020-09-17 11:18:18.017 30034-30034/com.ibrahimkiceci.simplynoteapp D/AndroidRuntime: Shutting down VM 2020-09-17 11:18:18.020 30034-30034/com.ibrahimkiceci.simplynoteapp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.ibrahimkiceci.simplynoteapp, PID: 30034 java.lang.NullPointerException: null cannot be cast to non-null type kotlin.String at com.ibrahimkiceci.simplynoteapp.ListViewActivity$getDataFromFirestore$1.onEvent(ListViewActivity.kt:114) at com.ibrahimkiceci.simplynoteapp.ListViewActivity$getDataFromFirestore$1.onEvent(ListViewActivity.kt:19) at com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal$2(Query.java:1142) at com.google.firebase.firestore.Query$$Lambda$3.onEvent(Unknown Source:6) at com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent$0(AsyncEventListener.java:42) at com.google.firebase.firestore.core.AsyncEventListener$$Lambda$1.run(Unknown Source:6) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 2020-09-17 11:18:18.086 30034-30034/com.ibrahimkiceci.simplynoteapp I/Process: Sending signal. PID: 30034 SIG: 9

When ı clicked my sign ın button, the app is closed, I shared the code screen,

How can ı fix it? Thank you!

class ListViewActivity : AppCompatActivity() { private lateinit var auth: FirebaseAuth private lateinit var db : FirebaseFirestore

var titleTextFromFB : ArrayList<String> = ArrayList()
var imageFromFB : ArrayList<String> = ArrayList()
var adapter: NoteAdapter? = null


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

    auth = FirebaseAuth.getInstance()
    db = FirebaseFirestore.getInstance()

    getDataFromFirestore()

    // recyclerview

    var layoutManager = LinearLayoutManager(this)
    recyclerView.layoutManager = layoutManager
    adapter = NoteAdapter(titleTextFromFB, imageFromFB)

    recyclerView.adapter = adapter

}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {

    val menuInflater = menuInflater
    menuInflater.inflate(R.menu.add_note, menu)

    return super.onCreateOptionsMenu(menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {

    if (item.itemId == R.id.add_note) {
        // Take Notes Activity
        val intent = Intent(applicationContext, TakeNotesActivity::class.java)
        startActivity(intent)

    } else if (item.itemId == R.id.log_out) {

        val alert = AlertDialog.Builder(this)

        alert.setTitle("Log out")
        alert.setMessage("Are you sure to logout from the app ?")
        alert.setPositiveButton("Yes") {dialog, which ->

            auth.signOut()
            val intent = Intent(applicationContext, MainActivity::class.java)
            startActivity(intent)
            finish()
        }

        alert.setNegativeButton("No") {dialog, which ->

        }

        alert.show()


    }


    return super.onOptionsItemSelected(item)
}

// get data from firestore

fun getDataFromFirestore() {

    db.collection("Notes").orderBy("date", Query.Direction.DESCENDING).addSnapshotListener { snapshot, exception ->

        if (exception != null) {

            // If there is a error ,

            Toast.makeText(applicationContext, exception.localizedMessage.toString(), Toast.LENGTH_LONG).show()


        } else {

            if (snapshot != null) {

                if (!snapshot.isEmpty) {

                    val documents = snapshot.documents
                    for (document in documents) {

                        val userEmail = document.get("userEmail") as String
                        val noteTitle = document.get("noteTitle") as String
                        val yourNote = document.get("yourNote") as String
                        val downloadUrl = document.get("downloadUrl") as String
                        val timestamp = document.get("date") as Timestamp
                        val date = timestamp.toDate()

                        titleTextFromFB.add(noteTitle)
                        imageFromFB.add(downloadUrl)

                        adapter!!.notifyDataSetChanged()




                    }
                }
            }


        }


      }



  }




}
13
  • can you paste the stack trace or line number where problem is coming? Also you can check yourself, If any of the string value in this code snippet is coming null or not, if it's null then add the ? operator on variable declaration. Commented Sep 17, 2020 at 8:54
  • I pasted the stack trace and ı also check the all null and ? operator but it looks like ok or I missed something Commented Sep 17, 2020 at 9:05
  • Check the line number 114, what is written over there? Commented Sep 17, 2020 at 9:23
  • When I click, it goes to Query and shows me this code : QuerySnapshot querySnapshot = new QuerySnapshot(this, snapshot, firestore); userListener.onEvent(querySnapshot, null); }; Commented Sep 17, 2020 at 9:50
  • Do you use some IDE for development if no then open in notepad or some other which shows the line number, you have error on line number 114, can you please tell that what is that line? From the code is not that clear what is the line exactly. Commented Sep 17, 2020 at 9:52

3 Answers 3

1

As I have understood, your errors are generating from this code block:

val userEmail = document.get("userEmail") as String
val noteTitle = document.get("noteTitle") as String
val yourNote = document.get("yourNote") as String
val downloadUrl = document.get("downloadUrl") as String

Here one of the variables from document is coming as null which cannot be cast to non-nullable String type in kotlin.

If you are not sure of the fields which can come null, write code like this:

val userEmail : String? = document.get("userEmail") as? String
val noteTitle : String?  = document.get("noteTitle") as? String
val yourNote : String? = document.get("yourNote") as? String
val downloadUrl : String? = document.get("downloadUrl") as? String

For more clarity please check Safe(nullable)-Cast operator in kotlin docs

Sign up to request clarification or add additional context in comments.

2 Comments

Your welcome, please accept and upvote it if you think this solved your problem.
No, ıt does not solve my problem. But thanks again for your interest, when ı find the solution , ı will share from here
0

The problem was solved when I deleted and reinstalled my database.

Comments

0

in your for loop

for (document in documents) {

                    val userEmail = document.get("userEmail") as String
                    val noteTitle = document.get("noteTitle") as String
                    val yourNote = document.get("yourNote") as String
                    val downloadUrl = document.get("downloadUrl") as String
                    val timestamp = document.get("date") as Timestamp
                    val date = timestamp.toDate()

                    titleTextFromFB.add(noteTitle)
                    imageFromFB.add(downloadUrl)

                    adapter!!.notifyDataSetChanged()




                }

one (or more) of the fields are null and you are trying to cast them to a not null String object! so in this case kotlin throws a runtime exception. you should cast it to String? instead of String: like so :

val field = document.get("field") as String?

or check its nullablity :

val field = if (document.get("field") != null) document.get("field") as String
    else ""

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.