-2
public class DatabaseHandler extends SQLiteOpenHelper {

   //COLUMNS OF THE NEW USER TABLE
    public static final String TABLE_NEWUSER = "newUser";
    public static final String COLUMN_NEWUSER_ID = "id";
    public static final String COLUMN_NEWUSER_NAME = "name";
    public static final String COLUMN_NEW_USER_PASSWORD = "password";
    public static final String COLUMN_NEW_USER_AGE = "age";

    //COLUMNS OF THE BALANCE TABLE
    public static final String COLUMN_BALANCE_ID = "id";
    public static final String TABLE_BALANCE = "balanceOfUser";
    public static final String COLUMN_BALANCE_DOLLARBALANCE = "dollarBalance";
    public static final String COLUMN_BALANCE_RUBBALANCE = "rubBalance";
    public static final String COLUMN_BALANCE_NEW_USER_ID = "newUserId";

    private static final String DATABASE_NAME = "webStore";
    private static final int DATABASE_VERSION = 1;

    private static final String SQL_CREATE_NEWUSER = "CREATE TABLE " + TABLE_NEWUSER + "("
            + COLUMN_NEWUSER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + COLUMN_NEWUSER_NAME + " TEXT NOT NULL, "
            + COLUMN_NEW_USER_PASSWORD + " TEXT NOT NULL, "
            + COLUMN_NEW_USER_AGE + " INTEGER NOT NULL"
            + ");";
    private static final String SQL_CREATE_BALANCE = "CREATE TABLE" + TABLE_BALANCE + "("
            + COLUMN_BALANCE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + COLUMN_BALANCE_DOLLARBALANCE + " INTEGER NOT NULL"
            + COLUMN_BALANCE_RUBBALANCE + " INTEGER NOT NULL"
            + COLUMN_BALANCE_NEW_USER_ID + " INTEGER NOT NULL"
            + ");";
    public DatabaseHandler(Context context){
        super(context,DATABASE_NAME,null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_BALANCE);
        db.execSQL(SQL_CREATE_NEWUSER);
        onCreate(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_BALANCE);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NEWUSER);
    }
  public DatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory,int version){
      super(context,DATABASE_NAME,factory,DATABASE_VERSION);
  }

}

public void CreateUser(View view) {
    etName = (EditText)findViewById(R.id.etName);
    etPassword = (EditText)findViewById(R.id.etPassword);
    etAge = (EditText)findViewById(R.id.etAge);
    String name = String.valueOf(etName.getText());
    String password = String.valueOf(etPassword.getText());
    int age = Integer.parseInt(String.valueOf(etAge.getText()));
    //write to database of user from our edit texts
    DatabaseHandler databaseHandler = new DatabaseHandler(this);
    Log.d("Insert: ", "Inserting ..");
    NewUserDAO dbForUser = new NewUserDAO(this);
    dbForUser.createNewUser(name,password,age);
}

I have 2 tables and in table balance i have key of user. When i create new user (createUser method) i have exception:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'long android.database.sqlite.SQLiteDatabase.insert(java.lang.String, java.lang.String, android.content.ContentValues)' on a null object reference.

Where is the problem? thanks

public class NewUserDAO {
public static final String TAG = "NewUserDAO";

//database fields
private SQLiteDatabase mDataBase;
private DatabaseHandler mDbHelper;
private Context mContext;
private String [] mAllColumns = {
        DatabaseHandler.COLUMN_NEWUSER_ID,
        DatabaseHandler.COLUMN_NEWUSER_NAME, DatabaseHandler.COLUMN_NEW_USER_PASSWORD,
        DatabaseHandler.COLUMN_NEW_USER_AGE };

public NewUserDAO(Context context){
    this.mContext = context;
    mDbHelper = new DatabaseHandler(context);
    try{
        open();
    } catch (SQLException e){
        Log.e(TAG,"SQLexception on opening database" + e.getMessage());
        e.printStackTrace();
    }
}
public void open() throws SQLException{
    mDataBase = mDbHelper.getWritableDatabase();
}
public void close(){
    mDbHelper.close();
}
public NewUserTable createNewUser(String name, String password, int age){
    ContentValues values = new ContentValues();
    values.put(DatabaseHandler.COLUMN_NEWUSER_NAME,name);
    values.put(DatabaseHandler.COLUMN_NEW_USER_PASSWORD,password);
    values.put(DatabaseHandler.COLUMN_NEW_USER_AGE, age);
    long insertId = mDataBase.insert(DatabaseHandler.TABLE_NEWUSER, null, values);
    Cursor cursor = mDataBase.query(DatabaseHandler.TABLE_NEWUSER, mAllColumns,
            DatabaseHandler.COLUMN_NEWUSER_ID + " = " + insertId, null, null, null, null);
    cursor.moveToFirst();
    NewUserTable newUser = cursorToUser(cursor);
    cursor.close();
    return newUser;
}

public List<NewUserTable> getAllUsers(){
    List<NewUserTable> listUsers = new ArrayList<>();
    Cursor cursor = mDataBase.query(DatabaseHandler.TABLE_NEWUSER, mAllColumns, null,null,null,null,null);
    if (cursor != null) {
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            NewUserTable nut = cursorToUser(cursor);
            listUsers.add(nut);
            cursor.moveToNext();
        }
        cursor.close();
    }
    return listUsers;
}
public NewUserTable getUserById (long id){
    Cursor cursor = mDataBase.query(DatabaseHandler.TABLE_NEWUSER, mAllColumns, DatabaseHandler.COLUMN_NEWUSER_ID + " = ?",
            new String[]{String.valueOf(id)}, null, null, null );
    if ( cursor !=  null){
        cursor.moveToFirst();
    }
    NewUserTable nut = cursorToUser(cursor);
    return nut;
}
protected NewUserTable cursorToUser (Cursor cursor){
    NewUserTable nut = new NewUserTable();
    nut.setID(cursor.getLong(0));
    nut.setName(cursor.getString(1));
    nut.setPassword(cursor.getString(2));
    nut.setAge(cursor.getInt(4));
    return nut;
}

}

Алексей
  • 37
  • 1
  • 1
  • 9
  • In `dbForUser.createNewUser` possibly? You have not initialized the SQLiteDatabase instance and are calling `db.insert` with a null `db` variable. Please read more of the logcat to see what class and line number that is happening on – OneCricketeer Feb 17 '16 at 20:01
  • 2
    `NewUserDAO.java:50` means line 50 of `NewUserDAO.java`, you have a null variable. Now, go fix it. This is a super common Java problem and really has nothing to with Android or SQLite. – OneCricketeer Feb 17 '16 at 20:07
  • 2
    You knew *When i create new user (createUser method) i have exception*, still didn't care to post that code? Voting for duplicate by the way. – Rohit5k2 Feb 17 '16 at 20:18
  • insertId, null, null, null, null); cursor.moveToFirst(); NewUserTable newUser = cursorToUser(cursor); cursor.close(); return newUser; – Алексей Feb 17 '16 at 20:25
  • Please **do not use comments** for code. Please find the appropriate edit link under your question if you would like to add code. – OneCricketeer Feb 17 '16 at 20:42

2 Answers2

0

You are getting the edittext value without anything stored in it. So the string name stores null value. After that you are using it in the query. Here the exception occurs as name is null,password and age are null.

Chandan kushwaha
  • 941
  • 6
  • 26
  • 1
    1) That is not the problem 2) You can type into edittexts, then later do findViewById and you can get an empty string, not null – OneCricketeer Feb 17 '16 at 19:59
0

try by removing NOT NULLin creating your table schema. and how did u handle the createNewUser(name,password,age) method?

Aelaf
  • 118
  • 1
  • 11