Reputation: 57
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()
}
}
}
}
}
}
}
Upvotes: 0
Views: 3248
Reputation: 168
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 ""
Upvotes: 0
Reputation: 1148
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
Upvotes: 1