My app is crashing as soon as i click on the submit button on kotlin(android development) and i am using sqlite for the backend

32 Views Asked by At

So i am working on the app development and i made a simple app where you enter the email and password and its validate whether the user is present or not but as soon as i click on the submit button the app crashes.

So i tried making the new app again but the error persist. I just want to know the error.

my xml file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editTextTextEmailAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textEmailAddress"
        app:layout_constraintBottom_toTopOf="@+id/editTextTextPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.554" />

    <EditText
        android:id="@+id/editTextTextPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="448dp"
        android:ems="10"
        android:inputType="textPassword"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextTextPassword"
        app:layout_constraintVertical_bias="0.145" />
</androidx.constraintlayout.widget.ConstraintLayout>

my .kt file:

package com.example.myapplication
import MyDatabaseHelper
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast

class MainActivity : AppCompatActivity() {
    private lateinit var dbHelper: MyDatabaseHelper
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var email=findViewById<EditText>(R.id.editTextTextEmailAddress)
        var password=findViewById<EditText>(R.id.editTextTextPassword)
        var submit=findViewById<Button>(R.id.button)
        submit.setOnClickListener{
            if(isUserExists(email.text.toString(),password.text.toString())){
                Toast.makeText(this,"Yes it okay",Toast.LENGTH_LONG)
            }
            else{
                Toast.makeText(this,"Yes it not okay",Toast.LENGTH_LONG)
            }
        }
    }
    private fun isUserExists(email: String, password: String): Boolean {
        val db = dbHelper.readableDatabase
        val cursor = db.query("user_credentials", null, "email=? AND password=?",arrayOf(email, password), null, null, null
        )
        val count = cursor .count
        cursor. close()
        return count > 0
    }
}

my databasehelperfile:

import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper

class MyDatabaseHelper(context: Context) :
    SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {

    companion object {
        private const val DATABASE_NAME = "my_database.db"
        private const val DATABASE_VERSION = 1

        private const val TABLE_NAME = "user_credentials"
        private const val COLUMN_ID = "_id"
        private const val COLUMN_EMAIL = "email"
        private const val COLUMN_PASSWORD = "password"

        private const val SQL_CREATE_TABLE =
            "CREATE TABLE $TABLE_NAME (" +
                    "$COLUMN_ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    "$COLUMN_EMAIL TEXT, " +
                    "$COLUMN_PASSWORD TEXT)"

        // Add constant for password hashing algorithm, if needed
        // private const val PASSWORD_HASH_ALGORITHM = "SHA-256"
    }

    override fun onCreate(db: SQLiteDatabase) {
        db.execSQL(SQL_CREATE_TABLE)
    }

    override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
        // Placeholder for database upgrade logic
        // Example: If you need to add new columns or modify table structure,
        // you can handle the upgrade here by dropping and recreating the table,
        // or by migrating data as needed.
        // You can also implement version-specific upgrade steps based on oldVersion and newVersion.
    }

    // Add method for password hashing, if needed
    // private fun hashPassword(password: String): String {
    //     // Implement password hashing logic here
    // }
}
1

There are 1 best solutions below

0
Akshay On

It appears that your app is crashing because you haven't initialized the dbHelper variable in your MainActivity. Since you are using MyDatabaseHelper to interact with the SQLite database, you need to initialize it before accessing the database.

To fix this issue, initialize the dbHelper variable in your onCreate method before using it.

dbHelper = MyDatabaseHelper(this) // Initialize dbHelper