0

database click here to see database image

my data is uploaded on firebase i want to read my data and add in arraylist dataset and then shown in recycler view but my app crashed and i think it is due to pass of null length array and i am new to kotlin but i think that below approach is workable in java help to find out the error or suggest me method.

Main Activity

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

    var dataSet = ArrayList<User>()
    var database = FirebaseDatabase.getInstance()
    var usersAdapter= updateAdapter(dataSet)
    var recyclerView = findViewById<RecyclerView>(R.id.recyclerViewId)
    recyclerView.adapter=usersAdapter

    database.getReference().child("Users").addValueEventListener(object: ValueEventListener {
        override fun onDataChange(snapshot: DataSnapshot) {
            Log.d("MAIN_ACTIVITY","onDataChange called")
            dataSet.clear()
            for(snapshot1: DataSnapshot in snapshot.children){
                var user: User? = snapshot1.getValue<User>()
                dataSet.add(user!!)
            }
            usersAdapter.notifyDataSetChanged()
        }

        override fun onCancelled(error: DatabaseError) {
            Log.d("MAIN_ACTIVITY","onCancelled called")
        }
    })

}

updateAdapter

class updateAdapter(var dataset: ArrayList): RecyclerView.Adapter<updateAdapter.userViewHolder>() {

class userViewHolder(view: View): RecyclerView.ViewHolder(view) {
    var username = view.findViewById<TextView>(R.id.username)
    var lastMsg = view.findViewById<TextView>(R.id.lastMsg)
    var msgTime = view.findViewById<TextView>(R.id.msgTime)
    var profile = view.findViewById<ImageView>(R.id.profile)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): userViewHolder {
    Log.d("MAIN_ACTIVITY","onCreate View HOlder")
    var view = LayoutInflater.from(parent.context).inflate(R.layout.row_conversation,parent,false)
    return userViewHolder(view)
}

override fun onBindViewHolder(holder: userViewHolder, position: Int) {
    Log.d("MAIN_ACTIVITY","onBLind ViewHolder")

    var user = dataset[position]
    holder.username.text = user.Uname
}

override fun getItemCount(): Int {
    return dataset.size
}

}

User

  data class User(
    var Uid :String?="",
    var Uname:String?="",
    var Unumber :String?= "",
    var Uimage :String?="")

ERROR

  E/Perf: Fail to get file list com.example.ranka
  E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length          of null array
Fail to get file list com.example.ranka
getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array

ERROR

 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ranka, PID: 27503
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ranka/com.example.ranka.Activities.MainActivity}: java.lang.InstantiationException: java.lang.Class<com.example.ranka.Activities.MainActivity> cannot be instantiated
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3404)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3636)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2222)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:228)
    at android.app.ActivityThread.main(ActivityThread.java:7782)
    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:981)
 Caused by: java.lang.InstantiationException: java.lang.Class<com.example.ranka.Activities.MainActivity> cannot be instantiated
    at java.lang.Class.newInstance(Native Method)
    at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
    at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1251)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3392)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3636) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2222) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:228) 
    at android.app.ActivityThread.main(ActivityThread.java:7782) 
    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:981) 

I/Process: Sending signal. PID: 27503 SIG: 9

  • Please edit your question and add your database structure as a screenshot. – Alex Mamo Feb 16 '21 at 12:54
  • image is added now pls help me with this problem it is very important – piyush meena Feb 16 '21 at 13:10
  • The problem with your code lies in the fact the names of the properties in your class are different from the ones in the database. Both must match. See the starting capital letter vs. lowercase letter. – Alex Mamo Feb 16 '21 at 13:58
  • bro that was not the problem because my database is working fine with upload/write firebase and i check also with updaring name i think problem is with my dataset array due to passing null length array list and i dont know how to resolve pls help this is very important for me – piyush meena Feb 16 '21 at 17:13
  • The write operation has nothing to do with the read operation. Check the duplicate and try to use that solution. – Alex Mamo Feb 16 '21 at 17:21
  • ok bro it is now working fine and thanks for helping and the problem is in main activity on first line i use abstract keyword so it crashed everytime can you explain why thats happening and what is use of abstract keyword – piyush meena Feb 16 '21 at 17:50
  • Good to hear that it's working. However, that sounds like you should ask another question that explains what isn't working the way you expect when using the `abstract` keyword. – Alex Mamo Feb 16 '21 at 17:56

0 Answers0