简体   繁体   中英

bind value at index 2 is null

I have two activities. Sending data from one activity to another through intent. Then I'm calling a function from database to show all the record of that particular row.
Here feeder_no and date are composite primary key. I don't have null value for any record still there's a error null value at index 2.

This is function definition in database:

//show feeder details date wise
public Cursor showFeederDateWiseDetails(int feeder_no, String date) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.rawQuery("Select * from " + TABLE_FEEDER_DETAILS + " where feeder_no = ? and date = ?", new String[]{String.valueOf(feeder_no), date});
    while (cursor.moveToFirst()){
        if(cursor.getCount()>0)
            return cursor;
    }
    return null;
}

Function call

Intent intent = getIntent();
try {
    feeder_no = Integer.parseInt(intent.getStringExtra("feeder_no"));
}catch (NumberFormatException e){
    Toast.makeText(getApplicationContext(), "....", Toast.LENGTH_SHORT).show();
}
String date = intent.getStringExtra("date");
Cursor answer = myDB.showFeederDateWiseDetails(feeder_no, date);
while (answer.moveToNext()) {
    t1.setText(answer.getString(1));
    t2.setText(answer.getString(2));
    t3.setText(answer.getString(3));
}

This is how I am getting value of date and feeder no from one acitivity

private void openFeederDetailsPage() {
    Intent intent = new Intent(this, FeederDetailsDateWise.class);
    intent.putExtra("feeder_no", getFeederNo);
    intent.putExtra("date", getDate);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

There are some issues with your code.

Since feeder_no and date are composite primary key then you should expect that the query will return at most 1 row, so there is no need for a loop after you get the cursor.
A simple call to cursor.moveToFirst() is enough to check if the cursor returned a row.

Then when you get the cursor with the call to showFeederDateWiseDetails() you must check only if it is null .
If it is not, then you get the column values (there is no need to call moveToFirst() again, it is already done previously).

Finally, get the column values not directly by their index (which is not safe in a query which returns many columns) but by getColumnIndex() using their name.
You should know that the index 1 you use in t1.setText(answer.getString(1)); returns the value of the 2nd column since the index is 0 based.

Also, you should move the code that calls showFeederDateWiseDetails() inside the try...catch block:

public Cursor showFeederDateWiseDetails(int feeder_no, String date) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("Select * from " + TABLE_FEEDER_DETAILS + " where feeder_no = ? and date = ?", new String[]{String.valueOf(feeder_no), date});
    if (cursor.moveToFirst()) return cursor;
    return null;
}
.........................................................................
Intent intent = getIntent();
try {
    feeder_no = Integer.parseInt(intent.getStringExtra("feeder_no"));
    String date = intent.getStringExtra("date");
    Cursor answer = myDB.showFeederDateWiseDetails(feeder_no, date);
    if (answer != null) {
        t1.setText(answer.getString(answer.getColumnIndex("column1")));
        t2.setText(answer.getString(answer.getColumnIndex("column2")));
        t3.setText(answer.getString(answer.getColumnIndex("column3")));
    }
    answer.close();
} catch (NumberFormatException e) {
    Toast.makeText(getApplicationContext(), "....", Toast.LENGTH_SHORT).show();
}

Change column1 , column2 and column3 to the actual names of the columns returned by your query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM