-1

DatabaseHelper.java

    public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME="register.db";
    public static final String TABLE_NAME="registeration";
    public static final String COL_1="USERNAME";
    public static final String COL_2="FirstName";
    SQLiteDatabase db;


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


        db.execSQL("CREATE TABLE " + TABLE_NAME + " (USERNAME TEXT,FirstName TEXT)");

    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        db.execSQL("DROP TABLE IF EXISTS" +TABLE_NAME); //Drop older table if exists
        onCreate(db);


    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

Mainactivity.java

public class MainActivity extends AppCompatActivity {

    SQLiteOpenHelper openHelper;
    SQLiteDatabase db;
    Button _btnreg;
    EditText _txtpass,_txtuser;


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

        openHelper = new DatabaseHelper(this);
        _txtuser = (EditText)findViewById(R.id.txtuser);
        _txtpass = (EditText)findViewById(R.id.txtpass);
        _btnreg=(Button)findViewById(R.id.btnreg);
        _btnreg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                db=openHelper.getWritableDatabase();

                String user=_txtuser.getText().toString();
                String pass=_txtpass.getText().toString();


                insertdata(user,pass);
                Toast.makeText(getApplicationContext(), "register successfully",Toast.LENGTH_LONG).show();

            }
        });
    }

    private void insertdata(String user, String pass) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(DatabaseHelper.COL_1, user);
        contentValues.put(DatabaseHelper.COL_2, pass);

    }
}

Error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vivekpc.myapplication/com.example.vivekpc.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.database.sqlite.SQLiteDatabase.execSQL(java.lang.String)' on a null object reference

Johny
  • 625
  • 2
  • 6
  • 26
  • Please add detailed description for your question and make your title more clear to understand. – Oğuzhan Döngül Sep 20 '17 at 11:50
  • 1
    Your `db` is null in the constructor of `DatabaseHelper` and once you fixed that, you will run into a stackoverflow as you call `onCreate` in `onCreate`. I'd advise you to take a step back and follow some tutorials about the basics of Java to get an idea of what you're really doing – 0xDEADC0DE Sep 20 '17 at 11:51

1 Answers1

3

You pass the argument "sqLiteDatabase" and are trying to use "db".

@Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
    db.execSQL("DROP TABLE IF EXISTS" +TABLE_NAME); //Drop older table if 
    exists
    onCreate(db);
}

You should change to something like this:

@Override
public void onCreate(SQLiteDatabase db) {
    String sql = "CREATE TABLE IF NOT EXISTS "+TABLE+"("
            + ID + " integer primary key autoincrement,"
            + NAME + " text,"
            + LAST_NAME + " text,"
            + PHONE + " text,"
            + EMAIL + " text,"
            + PHOTO + " BLOB,"
            + FAVORITE + " boolean"
            +")";
    db.execSQL(sql);
}

Note that you have to create a table then execute the SQL.

Rob
  • 2,243
  • 4
  • 29
  • 40