App crashes on button press
<p>i need help. It has been a week i research to solve my problem, and no solution found for me.</p>

<p>I already put some code and logically it will work.</p>

<p>My problem is i want to get a information from database (SQLite) and save it into a variable.</p>

<p>When i press the button(getLoginInfo) it will crash the Apps. Here are the code.</p>

<p>DBHandler.java</p>

<pre><code>package project.asyraf.sar;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHandler extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "kypsarv2.db";
public static final int DATABASE_VERSION = 1;

public DBHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

    String createTableLoginInfo = "CREATE TABLE loginInfo (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, password TEXT, accesslevel TEXT, owner TEXT);";
    db.execSQL(createTableLoginInfo);
    String createTableAdmin = "CREATE TABLE admin (id INTEGER PRIMARY KEY AUTOINCREMENT, adminName TEXT, adminIC);";
    db.execSQL(createTableAdmin);
    String createTableLect = "CREATE TABLE lecturer (id INTEGER PRIMARY KEY AUTOINCREMENT, lecturerName TEXT, lecturerIC);";
    db.execSQL(createTableLect);
    String createTableStud = "CREATE TABLE student (id INTEGER PRIMARY KEY AUTOINCREMENT, studentName TEXT, studentIC);";
    db.execSQL(createTableStud);
    String createTableReminder = "CREATE TABLE reminder (id INTEGER PRIMARY KEY AUTOINCREMENT, subject TEXT, activity TEXT, startDate DATE, endDate DATE, description TEXT, owner TEXT);";
    db.execSQL(createTableReminder);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS loginInfo");
    db.execSQL("DROP TABLE IF EXISTS admin");
    db.execSQL("DROP TABLE IF EXISTS lecturer");
    db.execSQL("DROP TABLE IF EXISTS student");
    db.execSQL("DROP TABLE IF EXISTS reminder");

    onCreate(db);
}

protected String owner, accessLevel;

public void getLoginInfo (String username, String password)
{
    Cursor cursor = this.getReadableDatabase().rawQuery("SELECT * FROM loginInfo WHERE username = ? AND password = ?", new String[] {username, password});
    cursor.moveToFirst();

    accessLevel = cursor.getString(3);
    owner = cursor.getString(4);

    cursor.close();
}
</code></pre>

<p>LoginMenu.java</p>

<pre><code> package project.asyraf.sar;

import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class LoginMenu extends AppCompatActivity {

DBHandler useDB = new DBHandler(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_menu);
}

public void closeApps (View view){
    finish();
}

public void loginTrial (View view) {

    final EditText getUsername = (EditText) findViewById(R.id.username);
    final EditText getPassword = (EditText) findViewById(R.id.password);

    String textUsername, textPassword = "";

    textUsername = getUsername.getText().toString();
    textPassword = getPassword.getText().toString();

    Intent loginSuccessLecturer = new Intent (this, LecturerDashboard.class);
    Intent loginSuccessStudent = new Intent (this, StudentDashboard.class);
    Intent loginSuccessAdmin = new Intent (this, AdminDashboard.class);

    //Cursor getLogin = useDB.getLoginData(textUsername, textPassword);

    useDB.getLoginInfo(textUsername, textPassword);

    String owner = "";
    String accessLevel = "";
    owner = useDB.owner;
    accessLevel = useDB.accessLevel;

    StringBuffer buffer = new StringBuffer();

    if (textUsername.equals("") || textPassword.equals(""))
    {
        Toast.makeText(getApplicationContext(), "Enter Username or Password", Toast.LENGTH_SHORT).show();
    }
    else if (textUsername.equals("admin") &amp;&amp; textPassword.equals("admin"))
    {
        startActivity(loginSuccessAdmin);
    }
    else
    {
        if (owner.equals(""))
        {
            Toast.makeText(getApplicationContext(), "User do not exist", Toast.LENGTH_SHORT).show();
        }
        else
        {
            if (accessLevel.equals("student"))
            {
                startActivity(loginSuccessStudent);
            }
            else if (accessLevel.equals("lecturer"))
            {
                startActivity(loginSuccessLecturer);
            }
        }
    }

    /*
    if (owner.equals(""))
    {
        Toast.makeText(getApplicationContext(), "No Data", Toast.LENGTH_SHORT).show();
    }
    else
    {
        //showMessage("Data",buffer.toString());
        if (!owner.equals("") &amp;&amp; accessLevel.equals("student"))
        {
            startActivity(loginSuccessAdmin);
        }
        else
            Toast.makeText(getApplicationContext(), "Login Error, Please Login Again", Toast.LENGTH_SHORT).show();

        if (textUsername.equals(usernameInfo) &amp;&amp; textPassword.equals(passwordInfo))
        {
            if (accessInfo.equals("admin"))
            {
                //startActivity(loginSuccessAdmin);
            }
            else if (accessInfo.equals("student"))
            {
                Toast.makeText(getApplicationContext(), "Login to Student", Toast.LENGTH_SHORT).show();
                //startActivity(loginSuccessStudent);
            }
            else if (accessInfo.equals("lecturer"))
            {
                //startActivity(loginSuccessLecturer);
            }
            else
                Toast.makeText(getApplicationContext(), "Access Error, Please Login Again", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(getApplicationContext(), "Login Error, Please Login Again", Toast.LENGTH_SHORT).show();
        }
    }*/
}
}
</code></pre>

<p>error log</p>

<pre><code>android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0                           
                                                   at project.asyraf.sar.DBHandler.getLoginInfo(DBHandler.java:144)
                                                   at project.asyraf.sar.LoginMenu.loginTrial(LoginMenu.java:44)
</code></pre>

<p>DBHandler.java:144 (accessLevel = cursor.getString(3);)</p>

<p>LoginMenu.java:44 (useDB.getLoginInfo(textUsername, textPassword);)</p>