0

i'm trying to make simple signup/login app which uses SQLite database, so far i got this code. This is my "registration" activity which should implement users name and pin(password) to database after pushing register button i guess, sadly the app crashes.

  EditText reName, rePin;
    Button reRegister;
    DatabaseHelper helper = new DatabaseHelper(this);

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

        reName = (EditText)findViewById(R.id.reName);
        rePin = (EditText)findViewById(R.id.rePin);
        reRegister = (Button)findViewById(R.id.reRegister);
        reRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String Imie = reName.getText().toString();
                String Haslo = rePin.getText().toString();

                Contacts c =new Contacts();
                c.setName(Imie);
                c.setPass(Haslo);

                helper.insertContacts(c);


                Intent intent = new Intent(SignUp.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }

Here's also my DatabaseHelper code, i guess that the problem is somewhere in this code, but i cant really find it. What should i do or where should i find solution for this? :)

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "contacts.db";
    private static final String TABLE_NAME = "contacts";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_PASS = "pass";
    SQLiteDatabase db;

    private static final String TABLE_CREATE = "create table contacts (id integer primary key not null , " +
            "name text not null, pass text not null);";

    public DatabaseHelper(Context context){
        super(context, DATABASE_NAME, null, 1);

    }

    public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        db.execSQL(TABLE_CREATE);
        this.db = db;

    }

    public void insertContacts(Contacts c){
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        String query = "select + from contacts";
        Cursor cursor = db.rawQuery(query, null);
        int count = cursor.getCount();
        values.put(COLUMN_ID, count);
        values.put(COLUMN_NAME, c.getName());
        values.put(COLUMN_PASS, c.getPass());
        db.insert(TABLE_NAME, null, values);
        db.close();
    }

    public String searchPass(String name) {
        db = this.getReadableDatabase();
        String query = "select * from "+TABLE_NAME;
        Cursor cursor = db.rawQuery(query, null);
        String a,b;
        b = "not found";
        if (cursor.moveToFirst()){
            do {
                a =cursor.getString(2);
                if (a.equals(name)){
                    b= cursor.getString(3);
                    break;
                }

            }
            while (cursor.moveToNext());
        }
        return b;
    }


    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        String query = "DROP TABLE IF EXISTS" + TABLE_NAME;
        db.execSQL(query);
        this.onCreate(db);

    }
}

@edit - Logcat errors

12-16 13:08:14.316 3541-3541/com.example.mateusz.sqllogowanie E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.example.mateusz.sqllogowanie, PID: 3541
                                                                                java.lang.NullPointerException: Attempt to invoke virtual method 'void android.database.sqlite.SQLiteDatabase.execSQL(java.lang.String)' on a null object reference
                                                                                    at com.example.mateusz.sqllogowanie.DatabaseHelper.onCreate(DatabaseHelper.java:36)
                                                                                    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:333)
                                                                                    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:238)
                                                                                    at com.example.mateusz.sqllogowanie.DatabaseHelper.insertContacts(DatabaseHelper.java:42)
                                                                                    at com.example.mateusz.sqllogowanie.SignUp$1.onClick(SignUp.java:36)
                                                                                    at android.view.View.performClick(View.java:6294)
                                                                                    at android.view.View$PerformClick.run(View.java:24770)
                                                                                    at android.os.Handler.handleCallback(Handler.java:790)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                    at android.os.Looper.loop(Looper.java:164)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
1
  • Please include the logcat in your question Commented Dec 16, 2017 at 18:01

2 Answers 2

1

Your SqliteDatabase object is null.

Change this:

  @Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    db.execSQL(TABLE_CREATE); //db is null here
    this.db = db;  
}

to:

  @Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    this.db = sqLiteDatabase;
    db.execSQL(TABLE_CREATE);
}

Also for in your insertContacts() method change query to:

String query = "select * from contacts";
Sign up to request clarification or add additional context in comments.

Comments

0

Just look into you query string insertContacts you are using + instead of * just change query to

String query = "select * from contacts";

1 Comment

Logcat is naw in main post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.