Home > Software design >  I cannot type a message when i click to send button app getting crushes (kotlin Chat App)
I cannot type a message when i click to send button app getting crushes (kotlin Chat App)

Time:04-17

Here Is LogCat, i have an edit text when i type some messages and then click send button my application is stopping and then getting crashes and i dont know how can i fix my problem i read the logcat i tried some of methods but its not work on me unfortunatly

2022-04-17 03:32:43.556 3513-3513/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
2022-04-17 03:32:43.556 3513-3513/? E/Zygote: v2
2022-04-17 03:32:43.556 3513-3513/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
2022-04-17 03:32:43.556 3513-3513/? E/Zygote: v2
2022-04-17 03:32:43.556 3513-3513/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
2022-04-17 03:32:46.256 3513-3513/com.ilkin.chatapplication E/EmojiFactory_jni: Failed to load libemoji.so: dlopen failed: library "libemoji.so" not found
2022-04-17 03:33:14.656 3513-3513/com.ilkin.chatapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.ilkin.chatapplication, PID: 3513
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
        at com.ilkin.chatapplication.ChatActivity.onCreate$lambda-1(ChatActivity.kt:80)
        at com.ilkin.chatapplication.ChatActivity.$r8$lambda$R1zq8tMQxrJ-jV9XcXdIAOx-zJQ(ChatActivity.kt)
        at com.ilkin.chatapplication.ChatActivity$$ExternalSyntheticLambda0.onClick(Unknown Source)
        at android.view.View.performClick(View.java:5052)
        at android.view.View$PerformClick.run(View.java:20162)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5753)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

Here ChatActivity - here is my chat Activity when i check logCat it lead me here


import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.EditText
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.*

class ChatActivity : AppCompatActivity() {
    //private lateinit var chatRecyclerView: RecyclerView
    //private lateinit var messageBox: EditText
    //private lateinit var sendButton: ImageView
    //private lateinit var messageAdapter: MessageAdapter
    //private lateinit var messageList: ArrayList<Message>
    //private lateinit var mDbRef: DatabaseReference


    var messageBox: EditText? = null
    var chatRecyclerView: RecyclerView? = null
    var sendButton: ImageView? = null
    var messageAdapter: MessageAdapter? = null
    var messageList: ArrayList<Message>? = null
    var mDbRef: DatabaseReference? = null

    var receiverRoom: String? = null
    var senderRoom: String? = null


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

        val intent = Intent()
        val name = intent.getStringExtra("name")
        val receiverUid = intent.getStringExtra("uid")

        val senderUid = FirebaseAuth.getInstance().currentUser?.uid
        mDbRef = FirebaseDatabase.getInstance().getReference()

        senderRoom = receiverUid   senderUid
        receiverRoom = senderUid   receiverUid

        supportActionBar?.title = name

        chatRecyclerView = findViewById(R.id.chatRecyclerView)
       var messageBox =  findViewById<EditText>(R.id.message)
        sendButton = findViewById(R.id.sentButton)
        messageList = ArrayList()
        messageAdapter = MessageAdapter(this,messageList!!)

        chatRecyclerView!!.layoutManager = LinearLayoutManager(this)
        chatRecyclerView!!.adapter = messageAdapter

        mDbRef!!.child("chats").child(senderRoom!!).child("messages")
            .addValueEventListener(object: ValueEventListener{
                override fun onDataChange(snapshot: DataSnapshot) {

                    messageList!!.clear()

                    for (postSnapshot in snapshot.children){
                        val message = postSnapshot.getValue(Message::class.java)
                        messageList!!.add(message!!)
                    }
                    messageAdapter!!.notifyDataSetChanged()
                }

                override fun onCancelled(error: DatabaseError) {

                }

            })


        // adding the message to database
        sendButton!!.setOnClickListener {
            val message = messageBox.text.toString()
            val messageObject = Message(message,senderUid)

            mDbRef!!.child("chats").child(senderRoom!!).child("messages").push()
                .setValue(messageObject).addOnSuccessListener {
                    mDbRef!!.child("chats").child(receiverRoom!!).child("messages").push()
                        .setValue(messageObject)
                }
            messageBox.setText("")
        }
    }

}

CodePudding user response:

It looks like messageBox isn't initialized in:

val message = messageBox.text.toString()

The reason is that you're introducing a new local variable here:

var messageBox =  findViewById<EditText>(R.id.message)

To initialize the messageBox field, remove the var keyword:

messageBox =  findViewById<EditText>(R.id.message)

CodePudding user response:

You have overrided messageBox object creation.

Just remove var from inside class object creation

var messageBox = findViewById<EditText>(R.id.message) to only

messageBox =  findViewById<EditText>(R.id.message)
  • Related