Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: the bind value at index 1 is null

Tags:

android

sql

I got an error

java.lang.IllegalArgumentException: the bind value at index 1 is null   

I researched it and I understand that this is sql statement error and I Am trying to pass an empty value.

I checked my code and fixed some errors and empty spaces in sql statement, but still this error appears.

  04-15 11:07:32.820: E/AndroidRuntime(2258): FATAL EXCEPTION: main
    04-15 11:07:32.820: E/AndroidRuntime(2258): Process: com.mad.naszlimerickmobile, PID: 2258
    04-15 11:07:32.820: E/AndroidRuntime(2258): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mad.naszlimerickmobile/com.mad.naszlimerickmobile.AddEditActivity}: java.lang.IllegalArgumentException: the bind value at index 1 is null
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at android.os.Handler.dispatchMessage(Handler.java:102)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at android.os.Looper.loop(Looper.java:136)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at android.app.ActivityThread.main(ActivityThread.java:5017)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at java.lang.reflect.Method.invokeNative(Native Method)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at java.lang.reflect.Method.invoke(Method.java:515)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at dalvik.system.NativeStart.main(Native Method)
    04-15 11:07:32.820: E/AndroidRuntime(2258): Caused by: java.lang.IllegalArgumentException: the bind value at index 1 is null
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:164)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at com.mad.naszlimerickmobile.NotesDB.getList(NotesDB.java:150)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at com.mad.naszlimerickmobile.AddEditActivity.onCreate(AddEditActivity.java:80)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at android.app.Activity.performCreate(Activity.java:5231)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
    04-15 11:07:32.820: E/AndroidRuntime(2258):     ... 11 more

AddEditActivity

public class AddEditActivity extends Activity implements OnKeyListener {

    private EditText notetitleEditText;
    private EditText notesEditText;
    private Spinner listSpinner;

    private NotesDB db;
    private boolean editMode;
    private String currentTabName = "";
    private Note note;

//  private Button cancelNoteBtn;
//  private Button saveNoteBtn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_edit_note);

        // get references to widgets
        listSpinner = (Spinner) findViewById(R.id.listSpinnerId);
        notetitleEditText = (EditText) findViewById(R.id.noteTitleEditText);
        notesEditText = (EditText) findViewById(R.id.noteContentEditText);

        // set listeners
        notetitleEditText.setOnKeyListener(this);
        notesEditText.setOnKeyListener(this);

        //get buttons 
    //  cancelNoteBtn=(Button) findViewById(R.id.);

        // get the database object
        db = new NotesDB(this);

        // set the adapter for the spinner
        ArrayList<List> lists = db.getLists();
        ArrayAdapter<List> adapter = new ArrayAdapter<List>(this,
                R.layout.spinner_list, lists);
        listSpinner.setAdapter(adapter);

        // get edit mode from intent
        Intent intent = getIntent();
        editMode = intent.getBooleanExtra("editMode", false);

        // if editing
        if (editMode) {
            // get task
            long noteId = intent.getLongExtra("noteId", -1);
            note = db.getNote(noteId);

            // update UI with task
            notetitleEditText.setText(note.getNoteTitle());
            notesEditText.setText(note.getNotes());
        }

        // set the correct list for the spinner
        long listID;
        if (editMode) { // edit mode - use same list as selected task
            listID = (int) note.getListId();
        } else { // add mode - use the list for the current tab
            currentTabName = intent.getStringExtra("tab");
            listID = (int) db.getList(currentTabName).getListId();
        }
        // subtract 1 from database ID to get correct list position
        int listPosition = (int) listID - 1;
        listSpinner.setSelection(listPosition);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_add_edit_menu, menu);
        return true;
    }

    public boolean onOptionsIteamSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menuSaveNote:
            saveToDB();
            this.finish();
            break;

        case R.id.menuCancelNote:
            this.finish();
            break;

        }
        return super.onOptionsItemSelected(item);

    }

    private void saveToDB() {
        int listId = listSpinner.getSelectedItemPosition() + 1;
        String title = notetitleEditText.getText().toString();
        String notes = notesEditText.getText().toString();

        if (title == null || title.equals("")) {
            return;
        }

        if (!editMode) {
            note = new Note();
        }

        note.setListId(listId);
        note.setNoteTitle(title);
        note.setNotes(notes);

        if (editMode) {
            db.updateNote(note);
        } else {
            db.insertNote(note);
        }

    }

    @Override
    public boolean onKey(View view, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
            // hide the soft Keyboard
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_BACK) {
            saveToDB();
            return false;
        }
        return false;
    }

}

NoteDB

public class NotesDB {

    public static final String DB_NAME = "nlnotes.db";
    public static final int DB_VERSION = 1;

    // LIST TABLE
    public static final String LIST_TABLE = "list";

    public static final String LIST_ID = "listId";
    public static final int LIST_ID_COL = 0;

    public static final String LIST_NAME = "list_name";
    public static final int LIST_NAME_COL = 1;

    // NOTE TABLE
    public static final String NOTE_TABLE = "note";

    public static final String NOTE_ID = "noteId";
    public static final int NOTE_ID_COL = 0;

    public static final String NOTE_LIST_ID = "listId";
    public static final int NOTE_LIST_ID_COL = 1;

    public static final String NOTE_TITLE = "noteTitle";
    public static final int NOTE_TITLE_COL = 2;

    public static final String NOTE_NOTES = "notes";
    public static final int NOTE_NOTES_COL = 3;

    public static final String NOTE_COMPLETED = "date_completed";
    public static final int NOTE_COMPLETED_COL = 4;

    public static final String NOTE_HIDDEN = "hidden";
    public static final int NOTE_HIDDEN_COL = 5;

    public static final String CREATE_LIST_TABLE = 
            "CREATE TABLE " + LIST_TABLE + "(" + 
            LIST_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + LIST_NAME + " TEXT    UNIQUE)";

    public static final String CREATE_NOTE_TABLE = 
            "CREATE TABLE " + NOTE_TABLE + "(" +
            NOTE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
            NOTE_LIST_ID + " INTEGER, " +
            NOTE_TITLE + " TEXT, " + 
            NOTE_NOTES + " TEXT, " + 
            NOTE_COMPLETED + " TEXT, " + 
            NOTE_HIDDEN + " TEXT)";

    public static final String DROP_LIST_TABLE = "DROP TABLE IF EXIST"
            + LIST_TABLE;

    public static final String DROP_NOTE_TABLE = "DROP TABLE IF EXIST"
            + NOTE_TABLE;

    public static class DBHelper extends SQLiteOpenHelper {

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

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(CREATE_LIST_TABLE);
            db.execSQL(CREATE_NOTE_TABLE);

            db.execSQL("INSERT INTO list VALUES (1, 'Personal')");
            db.execSQL("INSERT INTO list VALUES (2, 'Other')");

            db.execSQL("INSERT INTO note VALUES (1, 1, 'Your first note', "
                    + "'Touch to edit', '0', '0')");
            db.execSQL("INSERT INTO note VALUES (2, 1, 'Your second note ', "
                    + "'Touch to edit', '0', '0')");

        }

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

            Log.d("Note list", "Upgrading db from version" + oldVersion
                    + " to " + newVersion);

            Log.d("Note list", "Deleting all content!");
            db.execSQL(NotesDB.DROP_LIST_TABLE);
            db.execSQL(NotesDB.DROP_NOTE_TABLE);
            onCreate(db);

        }
    }

    private SQLiteDatabase db;
    private DBHelper dbhelper;

    public NotesDB(Context context) {
        dbhelper = new DBHelper(context, DB_NAME, null, DB_VERSION);
    }

    private void openReadableDB() {
        db = dbhelper.getReadableDatabase();
    }

    private void openWritableDB() {
        db = dbhelper.getWritableDatabase();
    }

    private void closeDB() {
        if (db != null)
            db.close();
    }

    // to many args?
    public ArrayList<List> getLists() {
        ArrayList<List> lists = new ArrayList<List>();
        openReadableDB();
        Cursor cursor = db.query(LIST_TABLE, null, null, null, null, null, null);

        while (cursor.moveToNext()) {
            List list = new List();
            list.setListId(cursor.getInt(LIST_ID_COL));
            list.setList_name(cursor.getString(LIST_NAME_COL));
            lists.add(list);

        }
        cursor.close();
        closeDB();
        return lists;

    }

    // to many args?
    public List getList(String list_name) {
        String where = LIST_NAME + "= ?";
        String[] whereArgs = { list_name };

        openReadableDB();
        Cursor cursor = db.query(LIST_TABLE, null, where, whereArgs, null,
                null, null);

        List list = null;
        cursor.moveToFirst();
        list = new List(cursor.getInt(LIST_ID_COL),
                cursor.getString(LIST_NAME_COL));

        cursor.close();
        this.closeDB();

        return list;
    }

    // //////////////////////////////////////////
    public ArrayList<Note> getNotes(String list_name) {
        String where = NOTE_LIST_ID + "= ? AND " + NOTE_HIDDEN + "!='1'";

        long listID = getList(list_name).getListId();
        String[] whereArgs = { Long.toString(listID) };

        this.openReadableDB();
        Cursor cursor = db.query(NOTE_TABLE, null, where, whereArgs, null,
                null, null);
        ArrayList<Note> notes = new ArrayList<Note>();
        while (cursor.moveToNext()) {
            notes.add(getNoteFromCursor(cursor));
        }
        if (cursor != null)
            cursor.close();
        this.closeDB();
        return notes;

    }

    public Note getNote(long noteId) {
        String where = NOTE_ID + "= ?";
        String[] whereArgs = { Long.toString(noteId) };

        this.openReadableDB();
        Cursor cursor = db.query(NOTE_TABLE, null, where, whereArgs, null,
                null, null);

        cursor.moveToFirst();
        Note note = getNoteFromCursor(cursor);
        if (cursor != null)
            cursor.close();
        this.closeDB();

        return note;
    }

    private static Note getNoteFromCursor(Cursor cursor) {
        if (cursor == null || cursor.getCount() == 0) {
            return null;
        } else {
            try {
                Note note = new Note(cursor.getInt(NOTE_ID_COL),
                        cursor.getInt(NOTE_LIST_ID_COL),
                        cursor.getString(NOTE_TITLE_COL),
                        cursor.getString(NOTE_NOTES_COL),
                        cursor.getString(NOTE_COMPLETED_COL),
                        cursor.getString(NOTE_HIDDEN_COL));
                return note;

            } catch (Exception e) {
                return null;
            }
        }
    }

    public long insertNote(Note note) {
        ContentValues cv = new ContentValues();
        cv.put(NOTE_LIST_ID, note.getNoteId());
        cv.put(NOTE_TITLE, note.getNoteTitle());
        cv.put(NOTE_NOTES, note.getNotes());
        cv.put(NOTE_COMPLETED, note.getCompletedDate());
        cv.put(NOTE_HIDDEN, note.getHidden());

        this.openWritableDB();
        long rowID = db.insert(NOTE_TABLE, null, cv);
        this.closeDB();

        return rowID;
    }

    public int updateNote(Note note) {
        ContentValues cv = new ContentValues();
        cv.put(NOTE_LIST_ID, note.getNoteId());
        cv.put(NOTE_TITLE, note.getNoteTitle());
        cv.put(NOTE_NOTES, note.getNotes());
        cv.put(NOTE_COMPLETED, note.getCompletedDate());
        cv.put(NOTE_HIDDEN, note.getHidden());

        String where = NOTE_ID + "= ?";
        String[] whereArgs = { String.valueOf(note.getNoteId()) };

        this.openWritableDB();
        int rowCount = db.update(NOTE_TABLE, cv, where, whereArgs);
        this.closeDB();

        return rowCount;
    }

    public int deleteTask(long noteId) {
        String where = NOTE_ID + "= ?";
        String[] whereArgs = { String.valueOf(noteId) };

        this.openWritableDB();
        int rowCount = db.delete(NOTE_TABLE, where, whereArgs);
        this.closeDB();

        return rowCount;
    }

}

I appreciate any help!

like image 825
sir_K Avatar asked Dec 19 '22 16:12

sir_K


1 Answers

Your problem is in this line:

 Cursor cursor = db.query(LIST_TABLE, null, where, whereArgs, null, null, null);

As your whereArgs is the parameter passed into the method, if this parameter is null, you'll attempt to bind null value to the ? in the where string - resulting in this error.

The proper way would be to have two different where clauses - one for null and another for non-null value, especially considering that the syntax is different. Something like this:

String whereNotNull = LIST_NAME + "= ?";
String whereNull = LIST_NAME + " IS NULL";
String[] whereArgs = { list_name };

openReadableDB();
Cursor cursor = whereArgs == null 
                ? db.query(LIST_TABLE, null, whereNull, null, null, null, null)
                : db.query(LIST_TABLE, null, whereNotNull, whereArgs, null, null, null);

The same applies to your getNotes method further down in the same class.

Also, as a side note, I strongly suggest that you specify column names in your query rather than passing null, even if you really do need all columns. If you do specify columns, then you are guaranteed that the column order in the cursor will be the same as your columns in the query method. If you just pass null for it, then then should be in the same order as they were created - but it'll be very easy to mess things up in the long run if you need to modify your database later on.

EDIT: Looking again at your AddEditActivity code, I suspect that you never expect to pass null into the method and that this code:

currentTabName = intent.getStringExtra("tab");
listID = (int) db.getList(currentTabName).getListId();

Assumes that the value is always there - and is a non-null String. As you clearly are getting null, I suggest you check where this intent is launched and how the String extra is put into it. You may further want to do two things:

  1. Check that currentTabName is != null before passing it to getList - and deal with error condition if it is.

  2. Put the name of the extra parameter (tab) in this case into a static final String variable and reference it from there - this way you exclude the possibility of misspelling it.

like image 153
Aleks G Avatar answered Dec 31 '22 15:12

Aleks G