HextechEnthusiast HextechEnthusiast - 5 months ago 97
Android Question

Android - Problems while fetching info from sql database

Greetings overflow community.

I'm still a newbie for Android develpment, so please bear with me.
I'm sorry if this is already in another thread. I couldn't find anything related.

By following a huge amount of tutorials for the last 2 weeks, i've been trying to create an app that register reports on a database with the 4 tables below. They're created inside the app

1 - User table

2 - Project table

3 - type 1 project table

4 - type 2 project table

My problem is in the login screen. I'm trying to search my username/password fields in the user table in order to confirm user access.

If my username/password entry is found inside the table, it will search on the project table for all the projects that this user already created and return it on a list on the next activity. When clicking the login button, it should the app freezes and crashes after some time. Could you guys lend me a hand to understand whats going on with it?

The only thing my logcat shows are that I have leaks in some SQLiteConnections, which I don't know how to solve... My database class is kinda huge atm so if you guys need anything to be posted in order to understand the problem, I'll post later.

What are gservices.db and networkstatistics.sqlite?

09-26 14:32:40.266 2022-2035/com.google.process.gapps W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.google.android.gsf/databases/gservices.db' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
09-26 14:33:05.120 2210-2222/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/data/com.google.android.gms/databases/networkstatistics.sqlite' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.


The method that freezes the app is the following:

public boolean search_access(user user){
String [] tabela_user = new String[]{"username","password"};
String compare_user = "COLUMN_USER_USERNAME = ?";
String [] whereargs = new String[]{user.getUsername()}


Cursor cursor = db.query(TABLE_USER, tabela_user, compare_user, null, null, null, null );

if(cursor.moveToFirst()) {
if(cursor.getString(0).equals(user.getPassword())) {
MainActivity.acesso = true;
} else{
CharSequence text = "Incorrect Username/Password!!";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(null, text, duration);
toast.show();
MainActivity.acesso = false;
}
}
return MainActivity.acesso;
}


This gives the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)' on a null object reference
at com.example.relatoriodeobras.projeto_db.busca_acesso(projeto_db.java:177)(which is the cursor line)


Thanks in advance!

Answer

Your code is really too complex for this kind of operation. Try to use this code:

String userName = user.getUsername();


Cursor cursor = db.query(TABLE_USER, new String[]{COLUMN_USER_PASSWORD}, "COLUMN_USER_USERNAME = ?", new String[]{userName}, null, null, null, null, null);

if(cursor.moveToFirst)
{
 if(cursor.getString(0).equals(user.getPassword())) MainActivity.acesso = true;
 else{
                        CharSequence text = "Incorrect Username/Password!!";
                        int duration = Toast.LENGTH_SHORT;

                        Toast toast = Toast.makeText(null, text, duration);
                        toast.show();
                        MainActivity.acesso = false;
                }
}